import { fn } from "https://deno.land/x/ddc_vim@v4.0.2/deps.ts";
const { reduce } = fn;
{func} is called for every item in {object}, which can be a
String
, List
or a Blob
. {func} is called with two
arguments: the result so far and current item. After
processing all items the result is returned.
{initial} is the initial result. When omitted, the first item in {object} is used and {func} is first called for the second item. If {initial} is not given and {object} is empty no result can be computed, an E998 error is given.
Examples:
echo reduce([1, 3, 5], { acc, val -> acc + val })
echo reduce(['x', 'y'], { acc, val -> acc .. val }, 'a')
echo reduce(0z1122, { acc, val -> 2 * acc + val })
echo reduce('xyz', { acc, val -> acc .. ',' .. val })
Can also be used as a method
:
echo mylist->reduce({ acc, val -> acc + val }, 0)