UNPKG

crocks

Version:

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

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