Add reduce iterables utility

This commit is contained in:
Evan Hahn 2021-06-28 16:46:33 -05:00 committed by GitHub
parent 7cf7b1fca5
commit 4495a1ac67
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 2 deletions

View file

@ -155,6 +155,18 @@ class MapIterator<T, ResultT> implements Iterator<ResultT> {
}
}
export function reduce<T, TResult>(
iterable: Iterable<T>,
fn: (result: TResult, value: T) => TResult,
accumulator: TResult
): TResult {
let result = accumulator;
for (const value of iterable) {
result = fn(result, value);
}
return result;
}
export function take<T>(iterable: Iterable<T>, amount: number): Iterable<T> {
return new TakeIterable(iterable, amount);
}