feat: add reduce for iterator

This commit is contained in:
dudaodong committed 2022-12-26 16:55:24 +08:00
1 parent b9f0854950
commit b06fb6736d
2 files changed
+19

No files matched your search

+11
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
}