UNPKG

crocks

Version:

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

51 lines (38 loc) 1.08 kB
/** @license ISC License (c) copyright 2017 original and current authors */ /** @author Ian Hofmann-Hicks (evil) */ var isChain = require('../core/isChain') var isFunction = require('../core/isFunction') var err = 'composeK: Chain returning functions of the same type required' // composeK : Chain m => ((y -> m z), (x -> m y), ..., (a -> m b)) -> a -> m z function composeK() { 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) } if(fns.length === 1) { return head } var tail = fns.slice(1).reduce(function (comp, fn) { if(!isFunction(fn)) { throw new TypeError(err) } return function(m) { if(!isChain(m)) { throw new TypeError(err) } return comp(m).chain(fn) } }, function (x) { return x; }) return function() { return tail(head.apply(null, arguments)) } } module.exports = composeK