crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
44 lines (31 loc) • 895 B
JavaScript
/** @license ISC License (c) copyright 2016 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
var isFunction = require('../core/isFunction')
var err = 'pipe: 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))
}
}
// pipe : ((a -> b), (b -> c), ..., (y -> z)) -> a -> z
function pipe() {
var fns = [], len = arguments.length;
while ( len-- ) fns[ len ] = arguments[ len ];
if(!arguments.length) {
throw new TypeError(err)
}
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 = pipe