Reduced List (Expression) — skLambda

Addon: skLambda · Category: Expression · Since: 1.0.0

Syntax

%objects% reduced (with|using) %object% from %object%

Description

Folds a whole list down to a single value with a two-argument lambda. The list is combined pairwise left-to-right: the first argument is the running result (the accumulator), the second is the next element. For `3, 5, 2` and an adding lambda this runs `add(3, 5)` → 8, then `add(8, 2)` → 10. Without a seed, a one-element list reduces to that element (the lambda is never called) and an empty list reduces to nothing. With `from %object%`, that value seeds the accumulator: the lambda runs for every element (`add(seed, first)` first), an empty list reduces to the seed, and the seed's type can differ from the elements' (e.g. fold a list of items into a text).

Examples

# {_add} = lambda (a: number, b: number) -> number: return {_a} + {_b}
set {_total} to {_nums::*} reduced with {_add}
set {_total} to {_nums::*} reduced with {_add} from 100   # 100 + every element

View source