Skip to content

Instantly share code, notes, and snippets.

@bekliev
Last active March 10, 2020 01:46
Show Gist options
  • Save bekliev/058b1e4838a635a6d8ccf45d90004619 to your computer and use it in GitHub Desktop.
Save bekliev/058b1e4838a635a6d8ccf45d90004619 to your computer and use it in GitHub Desktop.
sum function which returns another function
(() => {
console.clear();
console.log('sum is: ' + sum(1)(2)(3)(4));
function sum(a, b = 0) {
const res = a + b;
console.log({res, a, b});
fn = (b) => sum(res, b);
fn.toString = fn.valueOf = () => res;
return fn;
}
})();
(() => {
console.clear();
console.log('sum is: ' + sum(1,2,3)(4,5));
function sum(...nums) {
const res = nums.reduce((a, b) => a + b);
console.log({...nums, res});
fn = (...nums) => sum(res, ...nums);
fn.toString = fn.valueOf = () => res;
return fn;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment