crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
31 lines (24 loc) • 917 B
JavaScript
/** @license ISC License (c) copyright 2016 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
var array = require('../core/array')
var curry = require('../core/curry')
var isApply = require('../core/isApply')
var isArray = require('../core/isArray')
var isFunction = require('../core/isFunction')
var isSameType = require('../core/isSameType')
var map = array.map
var ap = array.ap
// liftA3 :: Applicative m => (a -> b -> c -> d) -> m a -> m b -> m c -> m d
function liftA3(fn, x, y, z) {
if(!isFunction(fn)) {
throw new TypeError('liftA3: Function required for first argument')
}
else if(!((isApply(x) || isArray(x)) && isSameType(x, y) && isSameType(x, z))) {
throw new TypeError('liftA3: Applys of same type required for last three arguments')
}
if(isArray(x)) {
return ap(z, ap(y, map(fn, x)))
}
return x.map(fn).ap(y).ap(z)
}
module.exports = curry(liftA3)