Proposal: add Control.Monad.whenJust ∷ (Monad m) ⇒ Maybe α→(α→ m ()) → m ()

Liyang HU haskell.org at liyang.hu
Wed Nov 30 08:11:32 CET 2011


This particular pattern crops up a lot for me. Does it for anyone else?

I (half-heartedly) propose adding to Control.Monad the following:

> -- | A variation of 'when' for 'Maybe a'.
> whenJust :: (Monad m) => Maybe a -> (a -> m ()) -> m ()
> whenJust =  flip (maybe (return ()))

so that I can write:

>     whenJust (Map.lookup k m) $ \ a -> do
>         ...

instead of say:

>     case Map.lookup k m of
>         Nothing -> return ()
>         Just a -> do
>             ...

which is not only nicer on the eyes, but also saves me a valuable level of 
indentation.

Discussion period: 2 weeks.

Cheers,
/Liyang




More information about the Libraries mailing list