aboutsummaryrefslogtreecommitdiff
path: root/reduce.go
diff options
context:
space:
mode:
Diffstat (limited to 'reduce.go')
-rw-r--r--reduce.go8
1 files changed, 8 insertions, 0 deletions
diff --git a/reduce.go b/reduce.go
new file mode 100644
index 0000000..46c186a
--- /dev/null
+++ b/reduce.go
@@ -0,0 +1,8 @@
+package collection
+
+func Reduce[T any, R any](collection []T, cb func(previous R, current T, idx int) R, accumulator R) R {
+ for i, v := range collection {
+ accumulator = cb(accumulator, v, i)
+ }
+ return accumulator
+}