1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-10 07:42:27 +08:00

feat: add reduce for iterator

This commit is contained in:
dudaodong
2022-12-26 16:55:24 +08:00
parent b9f0854950
commit b06fb6736d
2 changed files with 19 additions and 0 deletions

View File

@@ -101,3 +101,14 @@ func (iter *joinIterator[T]) HasNext() bool {
return result
}
// Reduce reduces iter to a single value using the reduction function reducer
func Reduce[T any, U any](iter Iterator[T], initial U, reducer func(U, T) U) U {
acc := initial
for item, ok := iter.Next(); ok; item, ok = iter.Next() {
acc = reducer(acc, item)
}
return acc
}