Last active
September 12, 2016 07:26
-
-
Save ivan-demchenko/3548972e0237cc3107a4d4af9e356afd to your computer and use it in GitHub Desktop.
Two most important functions for functional js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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