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

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

On 2019-04-10 11:36 a.m., James Ashwell 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
Or shorter mwhen = bool (const mempty) id I'd rather construct the right-hand side than memorize the left, so -1 from me.

On 10-04-19 14:04, Mario Blažević wrote:
On 2019-04-10 11:36 a.m., James Ashwell 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
Or shorter
mwhen = bool (const mempty) id
I'd rather construct the right-hand side than memorize the left, so -1 from me. Exactly, monoids usually don't operate on their own context as Monad/Applicatives do. It is better to see them as pure values dependent on `Bool`, for which Data.Bool.bool is a perfect function. Also the rhs is clearer and easier to remember.
A -1 from me. -- -- Rubén -- pgp: 4EE9 28F7 932E F4AD

My apologies, I've found a better way to express it and am retracting this proposal: mwhen cond a = fold [a | cond] a <> mwhen p b <> c <> mwhen q d = (fold.fold) [[a],[b|p],[c],[d|q]] This also works with other folds without touching monoid newtype wrappers. I had hang-ups about bool as I don't feel like you can 'read off' the ordering, and thought mempty might be a common argument , but neither of those apply here. Thank you for your replies.

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@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@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

On Wed, 10 Apr 2019, James Ashwell 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
https://hackage.haskell.org/package/utility-ht-0.0.14/docs/Data-Monoid-HT.ht...
participants (5)
-
David Feuer
-
Henning Thielemann
-
James Ashwell
-
Mario Blažević
-
Ruben Astudillo