-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathreduce.ts
26 lines (23 loc) · 902 Bytes
/
reduce.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import Sequence from "./Sequence";
export class Reduce {
/**
* Reduces the whole sequence to a single value by invoking `operation` with each element
* from left to right. For every invocation of the operation `acc` is the result of the last
* invocation. For the first invocation of the operation `acc` is the first element of the
* sequence.
*
* @param {(acc: S, value: T) => S} operation
* @returns {S}
*/
reduce<S, T extends S>(this: Sequence<T>, operation: (acc: S, value: T) => S): S {
const first = this.iterator.next();
if (first.done) {
throw new Error("Cannot reduce empty sequence");
}
let result: S = first.value;
for (let item = this.iterator.next(); !item.done; item = this.iterator.next()) {
result = operation(result, item.value);
}
return result;
}
}