Skip to content

Commit 215fbb1

Browse files
committed
feat: add curried variant of every function
1 parent ebe4000 commit 215fbb1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export const copy: <T>(array: ArrayLike<T>) => T[] =
99
? Array.from // tslint:disable-line:no-unbound-method
1010
: array => nativeSlice.call(array);
1111

12+
export function copyFn(): typeof copy {
13+
return copy;
14+
}
15+
1216
// tslint:disable-next-line:no-unbound-method
1317
const nativeMap = Array.prototype.map;
1418

@@ -18,6 +22,10 @@ export const map: <T, U>(array: ArrayLike<T>, f: (element: T, index: number) =>
1822
? Array.from as any // TypeScript 3.2 incorrectly requires this cast to any.
1923
: (array, f) => nativeMap.call(array, f);
2024

25+
export function mapFn<T, U>(f: (element: T, index: number) => U): (array: ArrayLike<T>) => U[] {
26+
return array => map(array, f);
27+
}
28+
2129
export function reverse<T>(array: ArrayLike<T>): T[] {
2230
const result = copy<T>({length: array.length});
2331
for (let i = 0; i < array.length; ++i) {
@@ -26,6 +34,10 @@ export function reverse<T>(array: ArrayLike<T>): T[] {
2634
return result;
2735
}
2836

37+
export function reverseFn(): typeof reverse {
38+
return reverse;
39+
}
40+
2941
export function groupBy<TElement>(array: ReadonlyArray<TElement>,
3042
keyOf: (element: TElement) => string): Dictionary<TElement[]> {
3143
const grouped = {} as Dictionary<TElement[]>;
@@ -40,6 +52,10 @@ export function groupBy<TElement>(array: ReadonlyArray<TElement>,
4052
return grouped;
4153
}
4254

55+
export function groupByFn<T>(keyOf: (element: T) => string): (array: ReadonlyArray<T>) => Dictionary<T[]> {
56+
return array => groupBy(array, keyOf);
57+
}
58+
4359
export function last<T>(array: ArrayLike<T>): T {
4460
if (array.length > 0) {
4561
return array[array.length - 1];
@@ -48,11 +64,19 @@ export function last<T>(array: ArrayLike<T>): T {
4864
}
4965
}
5066

67+
export function lastFn(): typeof last {
68+
return last;
69+
}
70+
5171
export function concatMap<T, U>(array: ArrayLike<T>, f: (element: T) => U[]): U[] {
5272
return map(array, f)
5373
.reduce((result, subarray) => result.concat(subarray), []);
5474
}
5575

76+
export function concatMapFn<T, U>(f: (element: T) => U[]): (array: ArrayLike<T>) => U[] {
77+
return array => concatMap(array, f);
78+
}
79+
5680
export function group<T>(elements: ReadonlyArray<T>, compare: Comparator<T>): T[][] {
5781
return copy(elements)
5882
.sort(compare)
@@ -73,7 +97,15 @@ export function group<T>(elements: ReadonlyArray<T>, compare: Comparator<T>): T[
7397
}, [] as T[][]);
7498
}
7599

100+
export function groupFn<T>(compare: Comparator<T>): (array: ReadonlyArray<T>) => T[][] {
101+
return array => group(array, compare);
102+
}
103+
76104
export function sum<T>(array: ArrayLike<T>, value: (element: T) => number): number {
77105
return map(array, value)
78106
.reduce((sum, value) => sum + value, 0);
79107
}
108+
109+
export function sumFn<T>(value: (element: T) => number): (array: ReadonlyArray<T>) => number {
110+
return array => sum(array, value);
111+
}

0 commit comments

Comments
 (0)