Proposal: Add Data.Monoid.mwhen :: Monoid m => Bool -> m -> m

David Feuer david.feuer at gmail.com
Wed Apr 10 18:14:22 UTC 2019


The symmetry appeals to me. However, `when` is mostly for convenience: I
mostly use it to reduce indentation in `do` notation. I don't see where I
would find `mwhen` particularly convenient. So -0.5 from me for now, but I
could be swayed by more persuasive examples.

On Wed, Apr 10, 2019, 11:36 AM James Ashwell <
james.michael.ashwell at gmail.com> wrote:

> This would mimic the behaviour of Control.Monad.when for monoids rather
> than applicatives.
>
> mwhen :: Monoid m => Bool -> m -> m
>
> mwhen b a
>
> | b         = a
>
> | otherwise = mempty
>
>
>
> -- Examples:
>
> when :: Applicative f => Bool -> f () -> f ()
>
> when b = getAp . mwhen b . Ap
>
> -- mwhen b = getConst . when b . Const
>
>
>
> guard :: Alternative f => Bool -> f ()
>
> guard b = getAlt (mwhen b (pure ()))
>
>
>
> applyIf :: Bool -> (a -> a) -> (a -> a)
>
> applyIf b = appEndo . mwhen b . Endo
>
>
>
> -- Using Monoid m => Monoid (a -> m)
>
> -- mwhen b f x   = if b then f x   else mempty = mwhen b (f x)
>
> -- mwhen b f x y = if b then f x y else mempty = mwhen b (f x y)
>
> -- etc
>
>
>
> -- mwhen b x <> y = if b then x <> y else y
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20190410/acaf51c5/attachment.html>


More information about the Libraries mailing list