Skip to content

Instantly share code, notes, and snippets.

@venil7
Last active May 4, 2018 19:50
Show Gist options
  • Save venil7/2a58cce487d95a90a0c9960a894d695b to your computer and use it in GitHub Desktop.
Save venil7/2a58cce487d95a90a0c9960a894d695b to your computer and use it in GitHub Desktop.
Unlimited arity curried computation
const aux = (f: (a,b) => number, acc: number) =>
(n?: number) => (n !== undefined) ? aux(f, f(acc, n)) : acc;
const add = (a, b) => a + b;
const mul = (a, b) => a * b;
const sum = aux(add, 0);
const product = aux(mul, 1);
const sum1 = sum(2)(3)(5)(); //10
const prod1 = product(2)(3)(5)(); // 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment