UNPKG

crocks

Version:

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

36 lines (26 loc) 986 B
/** @license ISC License (c) copyright 2017 original and current authors */ /** @author Ian Hofmann-Hicks (evil) */ var First = require('.') var Either = require('../core/types').proxy('Either') var curry = require('../core/curry') var isFunction = require('../core/isFunction') var isSameType = require('../core/isSameType') var applyTransform = function (either) { return either.either(First.empty, First); } // eitherToFirst : Either b a -> First a // eitherToFirst : (a -> Either c b) -> a -> First b function eitherToFirst(either) { if(isFunction(either)) { return function(x) { var m = either(x) if(!isSameType(Either, m)) { throw new TypeError('eitherToFirst: Either returning function required') } return applyTransform(m) } } if(isSameType(Either, either)) { return applyTransform(either) } throw new TypeError('eitherToFirst: Either or Either returning function required') } module.exports = curry(eitherToFirst)