Proposal: Add Chris Done's "om" combinator to Control.Monad

John Wiegley johnw at fpcomplete.com
Thu Jul 25 21:16:57 CEST 2013


The function is:

    om f m = (m >>=) . flip f

Allowing this typical usage:

    om when (return True) $
        print "Hello"

Thus, it allows one to do away with the monadic variants of pure functions,
such as "whenM", "unlessM", etc.:

    whenM   = om when
    unlessM = om unless

"om" gets even more handy when you want to apply a monadic function to a
monadic value in yet another monad:

    >>> om for_ (return (Just True)) print
    True

Rather than the typical (which I must have written hundreds of times by now):

    mx <- return (Just x)
    for_ mx $ \x -> {- use x... -}


A rider to this proposal is to also add "nom = flip om", but I can live
without that one.  "om", however, is handy enough that I've started locally
defining in all the modules where I find myself now reaching for it.

-- 
John Wiegley
FP Complete                         Haskell tools, training and consulting
http://fpcomplete.com               johnw on #haskell/irc.freenode.net




More information about the Libraries mailing list