fold
function:
Fold all the items in a list into a new value.
The first item should be the list, the second one the initial value and the third one a function that create the fold.
If the fuinction has only two arguments, the initial value will not be set.
The function will accespt as input an hash with value
, index
and so_far
keys (if the previous run returned nothing, the so_far
will be empty).
Examples:
- running:
(fold [1, 10, 0.6], 100, (+ .index .so_far .value))
will give:114.6
Because the first argument is 100, and then the fold will add all the argument in the list, 100 + 1 + 10 + 0.6 = 114.6.
- running:
(fold [1, 10, 0.6], (? (number? .so_far) (+ .so_far .value) .value))
will give:11.6
Because ifso_far
is not a number, we started the fold, so we can return the value, hene this is a simple sum, 1 + 10 + 0.6 = 11.6.
- running:
(fold {}, 1, 2)
will return nothing