[Main page] [Overview]   /sefun /fold_left
SYNOPSIS
mixed fold_left( closure c, mixed args, mixed begin)
FILE
/kernel/simul_efun/array.h
DESCRIPTION
fold_left folds an array of items or a string over a given function it
gets at least two arguments (plus an optional third),
first, the function (this must be given as a lambda closure), which has
to be a function getting two arguments (first of type of begin,
second of type of the items in the list)
second, the list of items (an array, or a string) which you want to
fold over the function, and the optional third a begin-element, which
otherwise will be assumed as 0.
the list will be folded over the given function beginning with the begin-
element and the most left (first) item of the list, passing the
result as first argument and the next item as second argument to the
function again ... when there are no more items in the list, the result
is returned (if the list is empty, begin will be the result)
for example: you have a function int plus(int a, int b) { return a + b; }
fold_left( #'plus, ({ 3, 4, 5 }), 42 ); would return the value of the
expression: (((42 + 3) + 4) + 5) (whatever that is P-))
with this function many operators can be put to good use, for instance
max, min, +,*, (maybe even ->, if you're going through a list of objects,
for instance)