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

Ruben Astudillo ruben.astud at gmail.com
Wed Apr 10 18:10:42 UTC 2019


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


More information about the Libraries mailing list