UNPKG

crocks

Version:

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

54 lines (41 loc) 1.35 kB
/** @license ISC License (c) copyright 2017 original and current authors */ /** @author Ian Hofmann-Hicks (evil) */ var array = require('../core/array') var curry = require('../core/curry') var curryN = require('../core/curryN') var isApply = require('../core/isApply') var isArray = require('../core/isArray') var isFunction = require('../core/isFunction') var isFunctor = require('../core/isFunctor') var isInteger = require('../core/isInteger') var isSameType = require('../core/isSameType') var ap = array.ap var applyAp = function (x, y) { if(!(isSameType(x, y) && (isArray(y) || isApply(y)))) { throw new TypeError('liftN: Applys of same type are required') } if(isArray(x)) { return ap(y, x) } return x.ap(y) } function liftN(n, fn) { if(!isInteger(n)) { throw new TypeError('liftN: Integer required for first argument') } if(!isFunction(fn)) { throw new TypeError('liftN: Function required for second argument') } return curryN(n, function() { var args = [], len = arguments.length; while ( len-- ) args[ len ] = arguments[ len ]; if(!isFunctor(args[0])) { throw new TypeError('liftN: Applys of same type are required') } return args.slice(1, n).reduce( applyAp, args[0].map(function (x) { return curryN(n, fn)(x); }) ) }) } module.exports = curry(liftN)