Last active
October 27, 2017 14:09
-
-
Save DavidLazic/925e3db62a93f5d02ca5e0d7e4882e7c to your computer and use it in GitHub Desktop.
FP util functions
This file contains hidden or 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
const sequence = () => compose.apply(this, Array.prototype.slice.call(arguments).reverse()); | |
const pipe = (...fns) => x => fns.reduce((y, f) => f(y), x); | |
const compose = (fn, ...rest) => | |
(!rest.length && Boolean(fn)) ? | |
fn : | |
(prop, ...args) => | |
fn(compose(...rest)(prop, ...args), ...args); | |
function curry (fn) { | |
var args = Array.prototype.slice.call(arguments, 1); | |
return function () { | |
return fn.apply(this, args.concat(Array.prototype.slice.call(arguments, 0))); | |
} | |
} | |
var inc = curry((a, b) => a + b, 1, 2); | |
inc(6); | |
const curry = fn => (...args) => { | |
if (args.length < fn.length) { | |
return (...restArgs) => curry(fn)(...args, ...restArgs); | |
} | |
return fn.apply(this, args); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment