Which one makes more sense for you and why?

whenJust :: Applicative f => Maybe a -> (a -> f ()) -> f ()


whenJust :: (Applicative f, Monoid m) => Maybe a -> (a -> f m) -> f m


whenJust :: Monoid m => Maybe a -> (a -> m) -> m

That last one doesn't work for all Monads, for one it doesn't work with Either, but I still managed to find people using it.

Although I suppose someone could argue that instance Monoid m => Monoid (Either m) would be lawful, but it just adds headache without much benefit to the cause.

There already are too many lawful instances in Haskell that shouldn't really exist. instance Foldable (a,) is one of those. Or maybe Foldable itself is just bad design, I don't know.

I like the last one best, though the first is OK.
I like it best because it reads out the most simply to me for how I think the function works.

@NickolasGir I too like the last one, but it won’t work for all monads. However, there is a certain purity to it.

@pureevil I like the second one more.

It captures the semantics (I think) you want, while retaining the most flexibility.

Sign in to participate in the conversation
Functional Café

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!