crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
39 lines (28 loc) • 820 B
JavaScript
/** @license ISC License (c) copyright 2017 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
var isSameType = require('../core/isSameType')
var isSemigroupoid = require('../core/isSemigroupoid')
var err = 'pipeS: Semigroupoids of the same type required'
// pipeS : Semigroupoid s => (s a b, s b c, ..., s y z) -> s a z
function pipeS() {
var ms = [], len = arguments.length;
while ( len-- ) ms[ len ] = arguments[ len ];
if(!arguments.length) {
throw new TypeError(err)
}
var head =
ms[0]
if(!isSemigroupoid(head)) {
throw new TypeError(err)
}
if(ms.length === 1) {
return head
}
return ms.slice().reduce(function (comp, m) {
if(!isSameType(comp, m)) {
throw new TypeError(err)
}
return comp.compose(m)
})
}
module.exports = pipeS