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

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

On 11/30/2011 02:11 PM, Liyang HU wrote:
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 ()))
We already have it. See Data.Foldable.forM_

Mikhail Vorozhtsov
On 11/30/2011 02:11 PM, Liyang HU wrote:
whenJust :: (Monad m) => Maybe a -> (a -> m ()) -> m () We already have it. See Data.Foldable.forM_
Good point! Hadn't crossed my mind that Maybe is Foldable; kind of obvious
really.
Evan Laforge
I prefer it to Foldable.forM_ because it's explicit about the type it takes, forM_ sounds too much like a loop.
Well, forM_ is just mapM_ flipped, and shurely we map over Maybe all the time? I'm much less inclined towards my proposal now, with the possible proviso that some people might find Foldable confusing. Also, if we go down the route of whenJust, why not whenLeft and whenRight (and so on) too? Cheers, /Liyang

On 11/30/2011 11:51 PM, Liyang HU wrote:
Also, if we go down the route of whenJust, why not whenLeft and whenRight (and so on) too?
I'd rather add something like maybeLeft ∷ Either a b → Maybe a maybeLeft = either Just (const Nothing) to Data.Either

On Tue, Nov 29, 2011 at 11:11 PM, Liyang HU
This particular pattern crops up a lot for me. Does it for anyone else?
Yep, I have that in my personal library, and use it fairly often. I prefer it to Foldable.forM_ because it's explicit about the type it takes, forM_ sounds too much like a loop.

On 11/30/2011 11:26 PM, Evan Laforge wrote:
On Tue, Nov 29, 2011 at 11:11 PM, Liyang HU
wrote: This particular pattern crops up a lot for me. Does it for anyone else?
Yep, I have that in my personal library, and use it fairly often.
I prefer it to Foldable.forM_ because it's explicit about the type it takes, forM_ sounds too much like a loop. Well, it is a loop over collection, which in the case of Maybe can contain at most one element.

On Thu, 1 Dec 2011, Mikhail Vorozhtsov wrote:
On 11/30/2011 11:26 PM, Evan Laforge wrote:
On Tue, Nov 29, 2011 at 11:11 PM, Liyang HU
wrote: This particular pattern crops up a lot for me. Does it for anyone else?
Yep, I have that in my personal library, and use it fairly often.
I prefer it to Foldable.forM_ because it's explicit about the type it takes, forM_ sounds too much like a loop.
Well, it is a loop over collection, which in the case of Maybe can contain at most one element.
Hoogle even lists forM_ and for_ in the second and third position when searching for the signature "Maybe a -> (a-> m ()) -> m ()" Nonetheless I also needed whenJust recently and missed "for_"!
participants (5)
-
Evan Laforge
-
Felipe Almeida Lessa
-
Henning Thielemann
-
Liyang HU
-
Mikhail Vorozhtsov