flipped foldMap: (Foldable t, Monoid c) => t a -> (a -> c) -> c
It is similar to whenJust, but more pure, while whenJust is inherently effectful since it returns unit.
It can be used e.g. in the HTML AST of an Elm-style render function, when you want to optionally render something. In that case you'd wanna render nothing at all (mempty) when you get a Nothing.
In that case, the instantiation would be Maybe a -> (a -> HTML) -> HTML. If you had mbMyNumber, you could do flippedFoldMap mbMyNumber renderNumber instead of case mbMyNumber of Nothing -> mempty; Just x -> renderNumber x.
I am posting the issue here because I think the function is very general, and I was expecting whenJust to do just this. So maybe other people will have the same suspicion, and it would make sense to have this function next to the existing whenJust. The problem is, I don't know what to call this flipped foldMap.
On IRC, dsal made me aware that whenJust = flip traverse_. Maybe the fact that whenJust could be more general than it is, means that the general version should also be available? It seems weird to argue that flip traverse_ should be specialized but flip foldMap shouldn't. Either way, to have this added, we'd need a name, and the best name I can think of is whenJust, but it is taken. Would love to hear whether you think this function belongs here or not, and what it should be called.
ghci> :t flip traverse_ :: Monad m => Maybe a -> (a -> m ()) -> m ()
flip traverse_ :: Monad m => Maybe a -> (a -> m ()) -> m ()
:: Monad m => Maybe a -> (a -> m ()) -> m ()
Thanks for your library, it has been very useful.
flipped foldMap:
(Foldable t, Monoid c) => t a -> (a -> c) -> cIt is similar to
whenJust, but more pure, whilewhenJustis inherently effectful since it returns unit.It can be used e.g. in the HTML AST of an Elm-style render function, when you want to optionally render something. In that case you'd wanna render nothing at all (mempty) when you get a Nothing.
In that case, the instantiation would be
Maybe a -> (a -> HTML) -> HTML. If you hadmbMyNumber, you could doflippedFoldMap mbMyNumber renderNumberinstead ofcase mbMyNumber of Nothing -> mempty; Just x -> renderNumber x.I am posting the issue here because I think the function is very general, and I was expecting
whenJustto do just this. So maybe other people will have the same suspicion, and it would make sense to have this function next to the existingwhenJust. The problem is, I don't know what to call this flipped foldMap.On IRC, dsal made me aware that
whenJust = flip traverse_. Maybe the fact thatwhenJustcould be more general than it is, means that the general version should also be available? It seems weird to argue thatflip traverse_should be specialized butflip foldMapshouldn't. Either way, to have this added, we'd need a name, and the best name I can think of iswhenJust, but it is taken. Would love to hear whether you think this function belongs here or not, and what it should be called.Thanks for your library, it has been very useful.