diff --git a/index.ts b/index.ts index 93ee8145..fe47d0a6 100644 --- a/index.ts +++ b/index.ts @@ -9,6 +9,10 @@ export const copy: (array: ArrayLike) => T[] = ? Array.from // tslint:disable-line:no-unbound-method : array => nativeSlice.call(array); +export function copyFn(): typeof copy { + return copy; +} + // tslint:disable-next-line:no-unbound-method const nativeMap = Array.prototype.map; @@ -18,6 +22,10 @@ export const map: (array: ArrayLike, f: (element: T, index: number) => ? Array.from as any // TypeScript 3.2 incorrectly requires this cast to any. : (array, f) => nativeMap.call(array, f); +export function mapFn(f: (element: T, index: number) => U): (array: ArrayLike) => U[] { + return array => map(array, f); +} + export function reverse(array: ArrayLike): T[] { const result = copy({length: array.length}); for (let i = 0; i < array.length; ++i) { @@ -26,6 +34,10 @@ export function reverse(array: ArrayLike): T[] { return result; } +export function reverseFn(): typeof reverse { + return reverse; +} + export function groupBy(array: ReadonlyArray, keyOf: (element: TElement) => string): Dictionary { const grouped = {} as Dictionary; @@ -40,6 +52,10 @@ export function groupBy(array: ReadonlyArray, return grouped; } +export function groupByFn(keyOf: (element: T) => string): (array: ReadonlyArray) => Dictionary { + return array => groupBy(array, keyOf); +} + export function last(array: ArrayLike): T { if (array.length > 0) { return array[array.length - 1]; @@ -48,11 +64,19 @@ export function last(array: ArrayLike): T { } } +export function lastFn(): typeof last { + return last; +} + export function concatMap(array: ArrayLike, f: (element: T) => U[]): U[] { return map(array, f) .reduce((result, subarray) => result.concat(subarray), []); } +export function concatMapFn(f: (element: T) => U[]): (array: ArrayLike) => U[] { + return array => concatMap(array, f); +} + export function group(elements: ReadonlyArray, compare: Comparator): T[][] { return copy(elements) .sort(compare) @@ -73,7 +97,15 @@ export function group(elements: ReadonlyArray, compare: Comparator): T[ }, [] as T[][]); } +export function groupFn(compare: Comparator): (array: ReadonlyArray) => T[][] { + return array => group(array, compare); +} + export function sum(array: ArrayLike, value: (element: T) => number): number { return map(array, value) .reduce((sum, value) => sum + value, 0); } + +export function sumFn(value: (element: T) => number): (array: ReadonlyArray) => number { + return array => sum(array, value); +} \ No newline at end of file