Skip to content

Commit

Permalink
feat: add curried variant of every function
Browse files Browse the repository at this point in the history
  • Loading branch information
djcsdy committed Apr 19, 2019
1 parent ebe4000 commit 215fbb1
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export const copy: <T>(array: ArrayLike<T>) => 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;

Expand All @@ -18,6 +22,10 @@ export const map: <T, U>(array: ArrayLike<T>, 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<T, U>(f: (element: T, index: number) => U): (array: ArrayLike<T>) => U[] {
return array => map(array, f);
}

export function reverse<T>(array: ArrayLike<T>): T[] {
const result = copy<T>({length: array.length});
for (let i = 0; i < array.length; ++i) {
Expand All @@ -26,6 +34,10 @@ export function reverse<T>(array: ArrayLike<T>): T[] {
return result;
}

export function reverseFn(): typeof reverse {
return reverse;
}

export function groupBy<TElement>(array: ReadonlyArray<TElement>,
keyOf: (element: TElement) => string): Dictionary<TElement[]> {
const grouped = {} as Dictionary<TElement[]>;
Expand All @@ -40,6 +52,10 @@ export function groupBy<TElement>(array: ReadonlyArray<TElement>,
return grouped;
}

export function groupByFn<T>(keyOf: (element: T) => string): (array: ReadonlyArray<T>) => Dictionary<T[]> {
return array => groupBy(array, keyOf);
}

export function last<T>(array: ArrayLike<T>): T {
if (array.length > 0) {
return array[array.length - 1];
Expand All @@ -48,11 +64,19 @@ export function last<T>(array: ArrayLike<T>): T {
}
}

export function lastFn(): typeof last {
return last;
}

export function concatMap<T, U>(array: ArrayLike<T>, f: (element: T) => U[]): U[] {
return map(array, f)
.reduce((result, subarray) => result.concat(subarray), []);
}

export function concatMapFn<T, U>(f: (element: T) => U[]): (array: ArrayLike<T>) => U[] {
return array => concatMap(array, f);
}

export function group<T>(elements: ReadonlyArray<T>, compare: Comparator<T>): T[][] {
return copy(elements)
.sort(compare)
Expand All @@ -73,7 +97,15 @@ export function group<T>(elements: ReadonlyArray<T>, compare: Comparator<T>): T[
}, [] as T[][]);
}

export function groupFn<T>(compare: Comparator<T>): (array: ReadonlyArray<T>) => T[][] {
return array => group(array, compare);
}

export function sum<T>(array: ArrayLike<T>, value: (element: T) => number): number {
return map(array, value)
.reduce((sum, value) => sum + value, 0);
}

export function sumFn<T>(value: (element: T) => number): (array: ReadonlyArray<T>) => number {
return array => sum(array, value);
}

0 comments on commit 215fbb1

Please sign in to comment.