<div dir="ltr">I'm -1 on this, due to it just further obfuscating the fact that Data.Foldable.for_ already exists.</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, May 10, 2013 at 2:13 AM, Niklas Hambüchen <span dir="ltr"><<a href="mailto:mail@nh2.me" target="_blank">mail@nh2.me</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I would like to propose the addition of<br>
<br>
whenJust :: Monad m => Maybe a -> (a -> m ()) -> m ()<br>
whenJust (Just x) f = f x<br>
whenJust _ _ = return ()<br>
<br>
to Control.Monad, in the section<br>
<br>
"Conditional execution of monadic expressions"<br>
<br>
next to<br>
<br>
guard :: MonadPlus m => Bool -> m ()<br>
when :: Monad m => Bool -> m () -> m ()<br>
unless :: Monad m => Bool -> m () -> m ()<br>
<br>
<br>
Why?<br>
<br>
It would allow us to write more readable code and fit well into the<br>
group of similar functions of this style.<br>
<br>
Compare<br>
<br>
mUser <- lookupUser<br>
<br>
whenJust mUser email<br>
<br>
or<br>
<br>
whenJust mUser $ \user -> do<br>
putStrLn "Mailing!"<br>
email user<br>
<br>
with some currently available alternatives:<br>
<br>
<br>
case mUser of<br>
Just user -> do putStrLn "Mailing!"<br>
email user<br>
Nothing -> return ()<br>
<br>
(Default base case clutter.)<br>
<br>
<br>
import Data.Foldable<br>
<br>
forM_ mUser $ \user -> do<br>
putStrLn "Mailing!"<br>
email user<br>
<br>
(Not too intuitive/well-named here and "Ambiguous occurrence forM_"<br>
clash with Control.Monad.)<br>
<br>
Some more dissatisfying alternatives:<br>
<br>
<br>
maybe (return ()) (\user -> do putStrLn "Mailing!"<br>
email user<br>
) mUser<br>
<br>
<br>
flip (maybe (return ())) mUser $ \user -> do<br>
putStrLn "Mailing!"<br>
email user<br>
<br>
<br>
import Control.Monad.Trans.Maybe<br>
import Control.Monad.Trans (lift)<br>
<br>
_ <- runMaybeT $ return mUser >>= \user -> lift $ do<br>
putStrLn "Mailing!"<br>
email user<br>
return ()<br>
<br>
<br>
Alternative names:<br>
<br>
- withJust, analog to withFile and withForeignPtr<br>
<br>
Any comments?<br>
<br>
_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org">Libraries@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/libraries" target="_blank">http://www.haskell.org/mailman/listinfo/libraries</a><br>
</blockquote></div><br></div>