Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

God no. If your loop isn't a map, it's almost certainly a fold. If you free up your syntax to make them easier to produce then they show up everywhere. Sum is a menial example.


> If your loop isn't a map, it's almost certainly a fold.

...all loops can be represented as folds


Not strictly true, but all maps can be represented as a fold. That's not usually the best tool though.


To clarify this now that I'm not on my phone, folds may not express termination by themselves, they depend upon the type that they're folding up. More formally, folds are catamorphisms which operate on data, but you often need to use codata to generate the structures to fold over.


Why not use a loop instead though?


Because you need to track and update the temporary state. Maps and folds let you define your stepping algorithm separately.


In python? I wasn't aware that fold did anything other then iterate in order...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: