Skip to content

Instantly share code, notes, and snippets.

@ivan-demchenko
Last active September 12, 2016 07:26
Show Gist options
  • Save ivan-demchenko/3548972e0237cc3107a4d4af9e356afd to your computer and use it in GitHub Desktop.
Save ivan-demchenko/3548972e0237cc3107a4d4af9e356afd to your computer and use it in GitHub Desktop.
Two most important functions for functional js
/**
* Here are two essential functions for functional style in JavaScript.
* Unfortunately, functions are not curried by default in JS.
* Also, there is no function composition operator in JS.
* Thus, we have to implement these on our own.
**/
const comp = (...fns) => (...args) =>
fns.reduceRight((res, fn) => fn(...[].concat(res)), args);
const curryN = (num, fn) => (...args) =>
args.length < num
? Function.prototype.bind.apply(fn, [this, ...args])
: fn(...args);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment