diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2022-04-07 21:36:40 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2022-04-07 21:36:40 +0300 |
commit | 9cd6b0fca87679e570fbdd4942e3068feb3439fa (patch) | |
tree | 557bf877d71854480f4171e9dd324cebb5606056 /reduce.go |
initial
Diffstat (limited to 'reduce.go')
-rw-r--r-- | reduce.go | 8 |
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 +} |