UNPKG

crocks

Version:

A collection of well known Algebraic Datatypes for your utter enjoyment.

47 lines (33 loc) 957 B
/** @license ISC License (c) copyright 2016 original and current authors */ /** @author Ian Hofmann-Hicks (evil) */ var isFunction = require('../core/isFunction') var err = 'compose: Functions required' function applyPipe(f, g) { if(!isFunction(g)) { throw new TypeError(err) } return function () { var args = [], len = arguments.length; while ( len-- ) args[ len ] = arguments[ len ]; return g.call(null, f.apply(null, args)); } } // compose : ((y -> z), (x -> y), ..., (a -> b)) -> a -> z function compose() { var args = [], len = arguments.length; while ( len-- ) args[ len ] = arguments[ len ]; if(!arguments.length) { throw new TypeError(err) } var fns = args.slice().reverse() var head = fns[0] if(!isFunction(head)) { throw new TypeError(err) } var tail = fns.slice(1).concat(function (x) { return x; }) return tail.reduce(applyPipe, head) } module.exports = compose