Fold left reduce function javascript

broken image
broken image

This means that a reader with little experience will have an easier time understanding a use of a fold than code that uses explicit recursion. The reason is told in crisp clear terms in, page 93.įolds have regular, predictable behavior. If you are (like I am) even moderately proficient with recursive functions and know how to pass values to a continuation, why would you need to bend, tweak and express your algo through an exotic thing like fold right?

broken image

Why folds matterĪn introduction before we plunge in the real stuff. Unlike fold left (which has signed a suicidal pact to reach the end of the list or blow the stack in the effort) during the trip with fold right you may talk to the driver and tell it for example to fetch the result and get out of the loop. This makes fold right extremely versatile. It has a non-intuitive way to handle its own parameters (trying to follow the nested calls with paper and pen or with the stepper of DrRacket is kind of funny), but the operation you entrust it with (represented by the function f) gets performed outside the recursion call, which is the general thing you’d expect a recursive function to do. Way more intelligent than its cousin fold left. Gist: Fold right (aka reduce right) is the basic list recursion operator.

broken image