Skip to content

Commit 9a0b7b3

Browse files
committed
feat(scanMonoidRight): implement scanMonoidRight functions
1 parent 176748c commit 9a0b7b3

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,26 @@ export function scanRightFn<T, U>(f: (accumulator: U, element: T, index: number)
345345
return array => scanRight(array, f, initial);
346346
}
347347

348+
export function scanMonoidRight<T>(array: ArrayLike<T>, f: (accumulator: T, element: T, index: number) => T): T[] {
349+
if (array.length === 0) {
350+
return [];
351+
}
352+
353+
const result: T[] = copy({0: array[0], length: array.length});
354+
355+
for (let i = array.length - 2; i >= 0; --i) {
356+
result[i] = f(result[i + 1], array[i], i);
357+
}
358+
359+
return result;
360+
}
361+
362+
export function scanMonoidRightFn<T>(f: (accumulator: T,
363+
element: T,
364+
index: number) => T): (array: ArrayLike<T>) => T[] {
365+
return array => scanMonoidRight(array, f);
366+
}
367+
348368
export function keyBy<T>(array: ArrayLike<T>,
349369
f: (element: T) => string): Dictionary<T[]> {
350370
const dictionary = {} as Dictionary<T[]>;

0 commit comments

Comments
 (0)