Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

For for foldables

for is flip traverse.

forM is flip mapM.

And so on with for_, etc.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What about foldMap? It’s lonely.

flip foldMap = ?

>Solution :

It would be silly if every 2-argument function had a flipped counterpart. The reason the for variants are worthwhile is mainly that they play so nicely together with do notation, in a way that closely remembers e.g. Python loops.

main = do
  ...
  forM_ [0..9] $ \i -> do
     print i
     ...
  return ()

For foldMap you could still benefit from a syntax-heralding $+lambda on the RHS, however you wouldn’t be in a monad (at least not one used as such), wouldn’t use do notation, and probably would need some parentheses anyway. So there’s not much advantage to be had over the regular foldMap with a parenthesized function in the middle.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading