Function File: x = reduce (function, sequence,initializer)
Function File: x = reduce (function, sequence)

Implements the ’reduce’ operator like in Lisp, or Python. Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(@(x,y)(x+y), [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. If initializer is not given and sequence contains only one item, the first item is returned.

 reduce(@plus,[1:10])
 ⇒ 55
     reduce(@(x,y)(x*y),[1:7]) 
 ⇒ 5040  (actually, 7!)

Package: miscellaneous