Created
November 12, 2014 15:40
-
-
Save wfaler/70f20f9f27f9fc66bf61 to your computer and use it in GitHub Desktop.
Must be a better way? some configuration of MaybeT?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ioMaybe :: Maybe a -> (a -> IO (Maybe b)) -> IO (Maybe b) | |
ioMaybe (Just a) fn = fn a | |
ioMaybe Nothing _ = return Nothing |
@wfaler actually, I think such code is a smell that you should use a transformer (in this case MaybeT
).
At TO we use traverseM a lot, but I think it's just because we don't use transformer when we should.
I don't think I ever used that function in Haskell, that's why I would highly recommend try to put transformers in the mix.
I'm not sure if this is any more concise but it works:
import Control.Monad.Trans.Maybe
ioMaybe :: Maybe a -> (a -> IO (Maybe b)) -> IO (Maybe b)
ioMaybe ma f = runMaybeT $ mta >>= f'
where mta = MaybeT . return $ ma
f' = MaybeT . f
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@aloiscochard came up with the following:
traverseM f m = fmap join $ traverse f m