[Haskell-cafe] monadic plumbing

Evan Laforge qdunkan at gmail.com
Tue Feb 22 23:10:27 CET 2011

I have a function 'justm' for this specific case:

-- | This is sort of like a monad transformer, but the Maybe is on the inside
-- instead of the outside.
-- What I really want here is MaybeT, but it requres explicit lifting...
justm :: (Monad m) => m (Maybe a) -> (a -> m (Maybe b)) -> m (Maybe b)
justm op1 op2 = maybe (return Nothing) op2 =<< op1

Used like:

lookup_selnum :: (Cmd.M m) => Types.SelNum
    -> m (Maybe (ViewId, Types.Selection))
lookup_selnum selnum =
    justm Cmd.lookup_focused_view $ \view_id ->
    justm (State.get_selection view_id selnum) $ \sel ->
    return $ Just (view_id, sel)

As implied by the comment, I wanted to use MaybeT but thought all the
explicit lifting and wrapping in runMaybeT was too complicated.
However losing 'do' notation is not that great either, so maybe I
should give it another go.

More information about the Haskell-Cafe mailing list