Functions

reduce

Supported By

Syntax

[the] reduce of ( list , functionName [, initialValue] )

reduce ( list , functionName [, initialValue] )

List yields a list. FunctionName yields the name of a function. InitialValue yields any variant.

Examples

put reduce((1,2,3,4,5), "sum") -- prints 15
put reduce((1,2,3,4,5), "product", 1) -- prints 120

Description

First, the reduce function sets the initialValue, or empty if initialValue is not specified, to be its current value. Then, for each item in the list, the reduce function evaluates the specified functionName with the current value and the item from the list as its two arguments. The return value of the function becomes the new current value. Finally, the reduce function returns its current value.

Notes

The first time the reduce function evaluates the specified functionName, the first parameter will be the initialValue and the second parameter will be the first item in the list. To start with the first and second items in the list, do something like the following:

local myList as list is (1,2,3,4,5)
put reduce(tail(myList), "product", head(myList))

See Also

map, filter