Skip to main content
Module

x/denops_std/function/mod.ts>reduce

📚 Standard module for denops.vim
Go to Latest
function reduce
import { reduce } from "https://deno.land/x/denops_std@v6.4.0/function/mod.ts";

{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)

Parameters

denops: Denops
object: unknown
func: unknown
optional
initial: unknown

Returns

Promise<unknown>