Proposal: Add Data.Monoid.mwhen :: Monoid m => Bool -> m -> m
James Ashwell
james.michael.ashwell at gmail.com
Wed Apr 10 15:36:02 UTC 2019
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20190410/631d5c3b/attachment.html>
More information about the Libraries
mailing list