Skip to content

Instantly share code, notes, and snippets.

@buybackoff
Last active April 11, 2018 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buybackoff/a05f290eadecd900932d7609e51d0612 to your computer and use it in GitHub Desktop.
Save buybackoff/a05f290eadecd900932d7609e51d0612 to your computer and use it in GitHub Desktop.
sum.ts
let sum = (x?: number) => {
// the trick is to access inner from inside itself as `any` with `.sum` initialized to 0
// we know that the reference will be initialized
// we could certainly achieve the same effect by using `this.`
// but scoping with `this` is not intuitive
let inner;
inner = (x1?: number) => {
// need additional check for 0, e.g. calling sum(0)(1)(0)() is valid, same below
if (x1 || x1 === 0) {
inner.sum += x1;
return inner;
} else {
return inner.sum;
}
}
// a JS function is an object, we could dynamically add anything to it
inner.sum = 0;
// this is only for the first call sum(...)...()
if (x || x === 0) {
// if x is set, then we work with inner
return inner(x);
} else {
// this is possible only when the fist call is sum();
return 0;
}
}
console.log('SUM: ', sum(1)(2)(0)(3)());
// run this as `ts-node sum.ts`
// if ts-node is not installed, run `npm i ts-node -g`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment