Created
November 7, 2018 12:46
-
-
Save Shwartz/40060317ea1eb3f394c8ece68bada7fa to your computer and use it in GitHub Desktop.
Helpers for curry, compose, apply to make functional approach to code
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
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