Skip to content

Commit

Permalink
feat(compose): add partial(), update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Oct 16, 2018
1 parent 405cf51 commit 6851f2c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ This project is part of the

Functional composition helpers:

- [comp(...)](https://github.com/thi-ng/umbrella/tree/master/packages/compose/src/comp.ts)
- [compI(...)](https://github.com/thi-ng/umbrella/tree/master/packages/compose/src/comp.ts)
- [juxt(...)](https://github.com/thi-ng/umbrella/tree/master/packages/compose/src/juxt.ts)
- [comp()](https://github.com/thi-ng/umbrella/tree/master/packages/compose/src/comp.ts)
- [compI()](https://github.com/thi-ng/umbrella/tree/master/packages/compose/src/comp.ts)
- [juxt()](https://github.com/thi-ng/umbrella/tree/master/packages/compose/src/juxt.ts)
- [partial()](https://github.com/thi-ng/umbrella/tree/master/packages/compose/src/partial.ts)

## Installation

Expand Down
1 change: 1 addition & 0 deletions packages/compose/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./comp";
export * from "./juxt";
export * from "./partial";
35 changes: 35 additions & 0 deletions packages/compose/src/partial.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";

export function partial<A, T>(fn: (a: A, ...args: any[]) => T, a: A): (...args: any[]) => T;
export function partial<A, B, T>(fn: (a: A, b: B, ...args: any[]) => T, a: A, b: B): (...args: any[]) => T;
export function partial<A, B, C, T>(fn: (a: A, b: B, c: C, ...args: any[]) => T, a: A, b: B, c: C): (...args: any[]) => T;
export function partial<A, B, C, D, T>(fn: (a: A, b: B, c: C, d: D, ...args: any[]) => T, a: A, b: B, c: C, d: D): (...args: any[]) => T;
export function partial<A, B, C, D, E, T>(fn: (a: A, b: B, c: C, d: D, e: E, ...args: any[]) => T, a: A, b: B, c: C, d: D, e: E): (...args: any[]) => T;
export function partial<A, B, C, D, E, F, T>(fn: (a: A, b: B, c: C, d: D, e: E, f: F, ...args: any[]) => T, a: A, b: B, c: C, d: D, e: E, f: F): (...args: any[]) => T;
export function partial<A, B, C, D, E, F, G, T>(fn: (a: A, b: B, c: C, d: D, e: E, f: F, g: G, ...args: any[]) => T, a: A, b: B, c: C, d: D, e: E, f: F, g: G): (...args: any[]) => T;
export function partial<A, B, C, D, E, F, G, H, T>(fn: (a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H, ...args: any[]) => T, a: A, b: B, c: C, d: D, e: E, f: F, g: G, h: H): (...args: any[]) => T;
export function partial(fn, ...args: any[]) {
let [a, b, c, d, e, f, g, h] = args;
switch (args.length) {
case 1:
return (...xs: any[]) => fn(a, ...xs);
case 2:
return (...xs: any[]) => fn(a, b, ...xs);
case 3:
return (...xs: any[]) => fn(a, b, c, ...xs);
case 4:
return (...xs: any[]) => fn(a, b, c, d, ...xs);
case 5:
return (...xs: any[]) => fn(a, b, c, d, e, ...xs);
case 6:
return (...xs: any[]) => fn(a, b, c, d, e, f, ...xs);
case 7:
return (...xs: any[]) => fn(a, b, c, d, e, f, g, ...xs);
case 8:
return (...xs: any[]) => fn(a, b, c, d, e, f, g, h, ...xs);
default:
illegalArgs();
}
}

export const foo = partial((a: string, b: number) => a + b, "a");

0 comments on commit 6851f2c

Please sign in to comment.