Skip to content

Commit 7ed9045

Browse files
committed
feat(scan): implement scan functions
1 parent a2d89b0 commit 7ed9045

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,22 @@ export function concatMapFn<T, U>(f: (element: T) => U[]): (array: ArrayLike<T>)
293293
return array => concatMap(array, f);
294294
}
295295

296+
export function scan<T, U>(array: ArrayLike<T>, f: (accumulator: U, element: T, index: number) => U, initial: U): U[] {
297+
const result: U[] = copy({length: array.length});
298+
let accumulator = initial;
299+
300+
for (let i = 0; i < array.length; ++i) {
301+
result[i] = accumulator = f(accumulator, array[i], i);
302+
}
303+
304+
return result;
305+
}
306+
307+
export function scanFn<T, U>(f: (accumulator: U, element: T, index: number) => U,
308+
initial: U): (array: ArrayLike<T>) => U[] {
309+
return array => scan(array, f, initial);
310+
}
311+
296312
export function keyBy<T>(array: ArrayLike<T>,
297313
f: (element: T) => string): Dictionary<T[]> {
298314
const dictionary = {} as Dictionary<T[]>;

0 commit comments

Comments
 (0)