Skip to content

Instantly share code, notes, and snippets.

@wfaler
Created November 12, 2014 15:40
Show Gist options
  • Save wfaler/70f20f9f27f9fc66bf61 to your computer and use it in GitHub Desktop.
Save wfaler/70f20f9f27f9fc66bf61 to your computer and use it in GitHub Desktop.
Must be a better way? some configuration of MaybeT?
ioMaybe :: Maybe a -> (a -> IO (Maybe b)) -> IO (Maybe b)
ioMaybe (Just a) fn = fn a
ioMaybe Nothing _ = return Nothing
@nlim
Copy link

nlim commented Nov 12, 2014

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