I have a tuple with a Maybe Int
in the second place. If this value is nothing, then the entire tuple is useless and should be discarded. I'd like to sequence over the tuple to bring the Maybe context out of the second place to envelop the entire tuple.
The signature of what I'm looking to do might be:
Monad m => (a, mb) -> m(a, b)
but I was unable to find anything similar on Hoogle.
I've read Haskell -- Is there a monad sequence function for tuples?, but it does not address the need.
https://hackage.haskell.org/package/tuple-0.3.0.2/docs/Data-Tuple-Sequence.html can only handle the case where the tuples are all wrapped in monad. I.e., Monad m => (ma, mb) -> m(a, b)
.
I can achieve this with a lambda function that tests the second element of the tuple and produces the appropriate response, but I'm wondering if there's an existing tool that does the job.
This is just ordinary sequence
on pairs. The traversable is (,) a
, and the second element is the thing that gets traversed.
ghci> sequence ('a', Just 3)
Just ('a',3)
ghci> sequence ('a', Nothing)
Nothing