[Haskell-cafe] Use forM_ with Maybe, it's Foldable!
Felipe Almeida Lessa
felipe.lessa at gmail.com
Sun Jan 27 00:09:22 CET 2013
Herbert Valerio actually pointed out to me in PVT that funnily enough
I've actually *forgotten* and *rediscovered* this idiom! =)
On Sat, Jan 26, 2013 at 5:43 PM, Felipe Almeida Lessa
<felipe.lessa at gmail.com> wrote:
> Just wanted to share this little insight I had that I don't think is a
> common idiom. Often times I need to define:
> whenJust :: Monad m => Maybe a -> (a -> m b) -> m ()
> whenJust (Just x) f = f x
> whenJust Nothing _ = return ()
> It always bugged me that you can't find this function anywhere.
> Actually, you can find it on at least a couple of packages , but
> you can't find it on a standard place. Or can you?
> A few days ago I decided to hoogle the type of whenJust  and what I
> discovered is that
> import Data.Foldable (forM_)
> whenJust = forM_
> For example, instead of:
> mfoo <- doSomething
> case mfoo of
> Just foo -> ...
> Nothing -> return ()
> You may just write:
> forM_ mfoo $ \foo ->
> There you go. I hope this is useful to someone =).
>  http://holumbus.fh-wedel.de/hayoo/hayoo.html?query=whenJust
>  http://www.haskell.org/hoogle/?hoogle=Maybe%20a%20-%3E%20(a%20-%3E%20m%20b)%20-%3E%20m%20()
More information about the Haskell-Cafe