title: Pulling a Maybe out of a Tuple in Purescript
body: <p>I have a function that looks like this:</p>
  <pre class="lang-hs prettyprint-override"><code>import Data.Bifunctor (lmap)
  import Data.Int (fromString)
  
  keyToInt :: forall a. Tuple String a -&gt; Tuple (Maybe Int) a
  keyToInt = lmap fromString 
  </code></pre>
  <p>What I'd like is this type signature:</p>
  <pre class="lang-hs prettyprint-override"><code>keyToInt :: forall a. Tuple String a -&gt; Maybe (Tuple Int a)
  </code></pre>
  <p>This seems to work:</p>
  <pre class="lang-hs prettyprint-override"><code>keyToInt :: forall a. Tuple String a -&gt; Maybe (Tuple Int a)
  keyToInt tuple = extractFstMaybe $ lmap fromString tuple
    where
      extractFstMaybe :: forall a. Tuple (Maybe Int) a -&gt; Maybe (Tuple Int a)
      extractFstMaybe (Tuple (Just a) b) = Just $ Tuple a b
      extractFstMaybe (Tuple Nothing _) = Nothing
  </code></pre>
  <p>But it feels like there should be a more abstract/cleaner way to express this. If I were to take <code>extractMaybe</code> to it's natural conclusion, I might create something like</p>
  <pre class="lang-hs prettyprint-override"><code>-- The typing doesn't really work for this
  extractMaybe :: forall a b c d. (Tuple a b) -&gt; Maybe (Tuple c d)
  extractMaybe (Tuple (Just a) (Just b)) = Just $ Tuple a b
  extractMaybe (Tuple (Just a) b) = Just $ Tuple a b
  extractMaybe (Tuple a (Just b)) = Just $ Tuple a b
  extractMaybe (Tuple Nothing _) = Nothing
  extractMaybe (Tuple _ Nothing) = Nothing
  extractMaybe (Tuple a b) = Just $ Tuple a b
  </code></pre>
  <p>While reading about applicatives and bind, this is the sort of refactoring I would see a lot. I don't really have the experience yet to apply those lessons for myself.</p>
  <p>If I have a structure that may contain an ADT (An Array of <code>Either</code>s, or <code>Maybe</code>s, or <code>Array</code>s) and I want to define a way pulling those &quot;out,&quot; is there a generalized way to define that?</p>
  <hr />
  <p><a href="https://book.purescript.org/chapter7.html" rel="nofollow noreferrer">Purescript by Example</a> has an example with this type signature for a function:</p>
  <pre class="lang-hs prettyprint-override"><code>combineList :: forall f a. Applicative f =&gt; List (f a) -&gt; f (List a)
  </code></pre>
  <p>which is doing the same thing, only for a List. N-Tuples complicate this because every element can have a different type. If I call <code>extractMaybe</code> on a Tuple, I now a have a Tuple with any types <strong>except</strong> for <code>Maybe</code> (or I have <code>Nothing</code>).</p>
  <p>I could write the same thing for <code>Either</code> and if I use both, I could know I have a Tuple with any types <strong>except</strong> for <code>Maybe</code> and <code>Either</code> (or I have <code>Nothing</code>).</p>
  <p>I'm not even entirely sure this is the most helpful direction to go in. It seems helpful in situations like <code>mapMaybe</code> where you want to filter out all <code>Nothing</code>s and <code>Left</code>s while unwrapping those types as well.</p>