crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
42 lines (30 loc) • 875 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 = 'composeS: Semigroupoids of the same type required'
// composeS : Semigroupoid s => (s y z, s x y, ..., s a b) -> s a z
function composeS() {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
if(!arguments.length) {
throw new TypeError(err)
}
var ms =
args.slice().reverse()
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 = composeS