import { reduce } from "https://deno.land/x/itertools@v1.1.2/builtins.ts";
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([1, 2, 3, 4, 5], (x, y) => x + y, 0)
calculates
(((((0+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.
Difference between reduce()
and reduce\_()
: reduce()
requires an
explicit initializer, whereas reduce_()
will automatically use the first
item in the given iterable as the initializer. When using reduce()
, the
initializer value is placed before the items of the sequence in the
calculation, and serves as a default when the sequence is empty. When using
reduce_()
, and the given iterable is empty, then no default value can be
derived and undefined
will be returned.