fold


fold(fn, init, @rest xs, ...)
      

Return init if the list arguments xs, ... are empty. If they are not empty, apply the function fn to init and the heads of xs, ..., then recur with the value returned by fn in place of init and the tails of xs, ....

The list arguments should all have the same length. Fn should accept one more argument than the number of list arguments and return a single value. It should not mutate the lists.

Examples:


fold(^(init, x) init + x * x, 0, [1, 2, 3, 4, 5])
// 55

fold(pair, [], [1,2,3,4])
// []:1:2:3:4]

fold(`-`, 0, [1, 2, 3, 4])
// -10
      

Also see:

fold_left
fold_right
accumulate
map


Core Module Index | Contents