Skip to content

Instantly share code, notes, and snippets.

@DavidLazic
Last active October 27, 2017 14:09
Show Gist options
  • Save DavidLazic/925e3db62a93f5d02ca5e0d7e4882e7c to your computer and use it in GitHub Desktop.
Save DavidLazic/925e3db62a93f5d02ca5e0d7e4882e7c to your computer and use it in GitHub Desktop.
FP util functions
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