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