This is similar to how you handle infinite lists with foldr in Haskell, where everything is evaluated lazily. The left fold expression is equivalent to the following application: ((((( <> show 1).The initial value is an optional second argument: var sum. strings, which defines functions for working with JavaScript strings. For example with multiplication, early stopping at 0 would be (a,b) => if(a = 0) a else a * b. In JavaScript 1.8, arrays have methods called reduce (left fold) and reduceRight (right fold). So the general usage is exactly the same as normal folds, but f can decide to add early stopping. The recursion then stops early, if f does not evaluate it’s second parameter. The signature for foldRight is changed to take its zero value and the function’s second parameter by name: def foldRight(list: List)(z: => B)(f: (A, => B) => B): B = I don’t know if there are standard names for such a parameter, but the behaviour itself is usually called “early stopping” or “breaking out of iteration / recursion”.Īnother approach for this kind of optimization is shown in the Laziness chapter of “Functional Programming in Scala” (The “Red Book”). Possible names for the parameter would then be breakCondition, stopAt / stopIf (the latter may be also suited for a single value). If you want the stopping condition to be a separate parameter, I’d suggest to take a A => Boolean instead of an A, which makes the API more generic. So it is left-to-right, like our keep function.) Cameron Morland.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |