I have written a program to pull a Maybe
out of a pair:
deMaybe :: (a, Maybe b) -> Maybe (a, b)
deMaybe (_, Nothing) = Nothing
deMaybe (x,Just y) = Just (x, y)
I know that Maybe
is a monad and (,) a
is a functor (among other typeclasses). I'm wondering if there is a higher-level function I'm missing, such as:
commute :: (Functor f, Monad m) => f (m a) -> m (f a)
My question is: can I write deMaybe
with a more general type signature like that of the hypothetical commute
, acknowledging that I am trying to commute one functor past another? Can this be done using functions such as fmap
, >>=
, pure
, &c.?