Skip to content

Instantly share code, notes, and snippets.

@Shwartz
Created November 7, 2018 12:46
Show Gist options
  • Save Shwartz/40060317ea1eb3f394c8ece68bada7fa to your computer and use it in GitHub Desktop.
Save Shwartz/40060317ea1eb3f394c8ece68bada7fa to your computer and use it in GitHub Desktop.
Helpers for curry, compose, apply to make functional approach to code
const {assign} = Object;
const pipe = (fn, ...fns) => (param, ...staticArgs) => fns.reduce((acc, f) => f(acc), fn(param, ...staticArgs));
const compose = (...fns) => pipe(...fns.reverse());
const pipeAsync = (fn, ...fns) => (param, ...staticArgs) => fns.reduce((acc, f) => acc.then(_ => f(_, ...staticArgs)), fn(param, ...staticArgs));
const composeAsync = (...fns) => pipeAsync(...fns.reverse());
const apply = (...fns) => (...args) => fns.map(fn => fn(...args));
const curry = (fn, ...args) => (fn.length <= args.length) ? fn(...args) : (...more) => curry(fn, ...args, ...more);
const match = (guard) => (left = _ => _, right = _ => _) => (...args) => (..._) => guard(...args) ? right(..._, ...args) : left(..._, ...args);
const extract = (_) => (...methods) => methods.reduce((acc, method) => assign(acc, {[method]: (...args) => _[method](...args)}), {});
export {pipe, compose, curry, apply, match, extract, composeAsync, pipeAsync}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment