crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
36 lines (28 loc) • 1.02 kB
JavaScript
/** @license ISC License (c) copyright 2017 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
var Pair = require('../core/Pair')
var curry = require('../core/curry')
var isContravariant = require('../core/isContravariant')
var isFunction = require('../core/isFunction')
var isSameType = require('../core/isSameType')
var isSemigroupoid = require('../core/isSemigroupoid')
var valid = function (x, y) { return isSameType(x, y)
&& isSemigroupoid(x)
&& isContravariant(x)
&& isFunction(x.first)
&& isFunction(x.second); }
// fanout : m a b -> m a c -> m a (b, c)
function fanout(fst, snd) {
if(isFunction(fst) && isFunction(snd)) {
return function (x) { return Pair(fst(x), snd(x)); }
}
if(valid(fst, snd)) {
return fst.first()
.compose(snd.second())
.contramap(function (x) { return Pair(x, x); })
}
throw new TypeError(
'fanout: Arrows, Functions or Stars of the same type required for both arguments'
)
}
module.exports = curry(fanout)