From 535c91c4591d6984791efe70987faead19451b4a Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 6 Feb 2023 20:59:11 +1100 Subject: [PATCH] Update compound-library.md --- compound-library.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/compound-library.md b/compound-library.md index 4d76d68..adb8ecb 100644 --- a/compound-library.md +++ b/compound-library.md @@ -54,6 +54,21 @@ var a = [1, 2, 3]; print a.map(increment); //prints [2,3,4]; ``` +## _reduce(self, default, func: fn) + +This function takes either an array or a dictionary as the `self` argument, a default value, and a function as `func`. The argument `func` takes three arguments - the first is the accumulator, the second is the index/key and the third is the value. It applies the given function to every element, passing the result of each call as the accumulator to the next (the default value is used for the first call). Finally, the final value of the accumulator is returned to the caller. + +``` +import compound; + +fn f(acc, k, v) { + return acc + v; +} + +var a = [1, 2, 3, 4]; + +print a.reduce(0, f); //prints "10" +``` ## _toLower(self: string)