[Haskell-beginners] Maybe perform an action

Michael Snoyman michael at snoyman.com
Sun Dec 5 12:40:20 CET 2010


On Sun, Dec 5, 2010 at 1:23 PM, Tim Baumgartner
<baumgartner.tim at googlemail.com> wrote:
> Hi Haskellers,
>
> I have another simple question in order to improve my coding style.
> I'm using the following function, e.g. when a dialog returned Maybe
> NiceStuff:
>
> performMaybe :: Monad m ⇒ Maybe a → (a → m b) → m ()
> performMaybe x action = when (isJust x) (action (fromJust x) >> return ())
>
> example use:
> main = performMaybe (Just "Hello") print
>
> Now I wonder if others use such a function as well, if it's already
> defined in the standard libraries (didn't find it using Hoogle) and
> perhaps how to implement it a little nicer.

I often times do this as

maybe (return ())

If you want the exact same type signature, you just need to flip it:

flip (maybe $ return ())

Michael



More information about the Beginners mailing list