reduce
executes a user-supplied "reducer" expression on each element of the repeated type, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the repeated type is a single value.
range.reduce(accumulator, current, <expr>, <init>)
range
: repeated type valueaccumulator
: The value resulting from the previous<expr>
expression. On the first call, its value is the result of<init>
expression.current
: current iteration value<expr>
: expression for reduce operation<init>
: expression for initial value
[2, 3, 4].reduce(accum, cur, accum + cur, 1) //=> 10
Returns the element that evaluates to <expr>
when the result is true. If all elements are not matched, optional.none
is returned. Thus, the return value of first
will always be of optional type. The optional value can be used as is for field binding.
first
is equivalent to the following expression.
range.filter(var, <expr>)[?0]
range.first(var, <expr>)
range
: repeated type valuevar
: current iteration value<expr>
: Write a conditional expression to return the first match. Must return a boolean value.
[1, 2, 3, 4].first(v, v % 2 == 0) //=> optional.of(2)
Returns elements sorted in ascending order. Compares values evaluated by the expression.
range.sortAsc(var, <expr>)
range
: repeated type valuevar
: current iteration value<expr>
: expression for value to be compared
[4, 2, 3, 1].sortAsc(v, v) //=> [1, 2, 3, 4]
Returns elements sorted in descending order. Compares values evaluated by the expression.
range.sortDesc(var, <expr>)
range
: repeated type valuevar
: current iteration value<expr>
: expression for value to be compared
[4, 2, 3, 1].sortDesc(v, v) //=> [4, 3, 2, 1]
Returns elements sorted in ascending order. Compares values evaluated by the expression. The sorting algorithm is guaranteed to be stable.
range.sortStableAsc(var, <expr>)
range
: repeated type valuevar
: current iteration value<expr>
: expression for value to be compared
[4, 2, 3, 1].sortStableAsc(v, v) //=> [1, 2, 3, 4]
Returns elements sorted in descending order. Compares values evaluated by the expression. The sorting algorithm is guaranteed to be stable.
range.sortStableDesc(var, <expr>)
range
: repeated type valuevar
: current iteration value<expr>
: expression for value to be compared
[4, 2, 3, 1].sortStableDesc(v, v) //=> [4, 3, 2, 1]