Reference · Lists
reduce
reduce(list | null, any, fn) -> anyWhat it does
Folds the list into one value. The start value comes second and the function third:
reduce(xs, start, f), the other way round from JavaScript's xs.reduce(f, start).
The function takes two arguments, the value so far and the next item, and gives the
new value so far; the last of those is the result. null counts as an empty list.
Examples
print(reduce([1, 2, 3], 0, (acc, x) => acc + x))print(reduce([1, 2, 3], 1, (acc, x) => acc * x))print(reduce(["a", "b", "c"], "", (acc, s) => acc + s))print(reduce([1, 2, 3], {}, (m, x) => set(m, str(x), x * x)))66abc{ "1": 1, "2": 4, "3": 9 }Edge cases
- The start value is required: an empty list or
nullgives it back unchanged, and the function is not called. - The start value can be anything, a list or a map included. Values never change, so
the function returns a new one each round (
push,set,merge) and the last returned is the result. - The function takes exactly two parameters, in this order: the value so far, then the item.
- For a plain total, sum, min and max need no function.
print(reduce([], 10, (acc, x) => acc + x), reduce(null, "start", (acc, x) => acc + x))print(reduce([1, 2, 3], [], (acc, x) => push(acc, x * 2)))print(reduce([3, 1, 2], null, (best, x) => best == null || x > best ? x : best))print(reduce(["a", "bb"], 0, (n, s) => n + len(s)))10 start[2, 4, 6]33Errors
A list that is not a list or null, or a function that is not a function, is a
runtime error, not an error value. Leaving the start value out gives the function to
the second parameter and nothing to the third, so that is a runtime error too. An
error inside the function stops the request.
print(reduce([1, 2], (acc, x) => acc + x))line 1: `reduce` expects a function as argument 3, got nothingprint(reduce([1, 2], 0, (acc) => acc))line 1: `<fn>` takes 1 argument, got 2Chaining
In a pipeline the list comes first and the start value and the function follow:
xs | reduce(start, f).
print([3, 1, 2] | reduce(0, (acc, x) => acc + x))print(["pear", "fig"] | map(len) | reduce(0, (acc, n) => acc + n))print(range(1, 6) | reduce(1, (acc, x) => acc * x))67120See also
sum, map, filter, push, set, group_by
Thread 0 posts
On reduce(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo