Open chshersh opened 6 years ago
I had an idea of doing the same for pass
, but I am not sure whether it's reasonable.
When comparing
whenJust :: forall m f . (Applicative f, Monoid m) => Maybe a -> (a -> f m) -> f m
and
whenJust :: Monoid m => Maybe a -> (a -> m) -> m
note that mempty
and pure mempty
need not be equal, so these functions can behave differently for some instantiations of f
.
The reason for having whenJust :: Monoid m => Maybe a -> (a -> m) -> m
type is because sometimes we want something like foldMap
for Maybe
but nobody came up with good name for this function.
Currently
whenJust
has the following type signature:I propose to generalize types of
whenJust
andwhenJustM
functions to the following:or even following:
()
has trivialMonoid
instance so every existing code will work automatically. Also,IO
hasMonoid
instance.Drawbacks:
Advantages
foldMap :: (Monoid m, Foldable t) => (a -> m) -> t a -> m
behavior forMaybe
.foo <- if p then listDirectory else pure []
shorter. Sometimes onNothing
case we want not onlypure ()
but return somemempty