Haskell conditional for | Hugonweb Annotated Link Bibliography
https://entropicthoughts.com/non-obvious-haskell-idiom-conditional-for
To only perform a monadic or applicative action if a value exists, you can use
$ action for_ value
So for example, if x
is a Maybe String, you could print it if it exists with:
$ putStrLn x for_ x
I think that's the beauty of Haskell, this thing with a name from lists, when generalized to (Traversable) Applicatives, can do useful things for all kinds of things.
Since for_
is just traverse_
with it's arguments flipped, you could also do:
traverse_ (action) value
Keep in mind for_
and traverse_
are in Data.Foldable
and not present in the Prelude.
Also, oddly, is the linked blog post scheduled to be published in the future?