crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
41 lines (30 loc) • 1.15 kB
JavaScript
/** @license ISC License (c) copyright 2017 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
var Async = require('.')
var Maybe = require('../core/types').proxy('Maybe')
var curry = require('../core/curry')
var isFunction = require('../core/isFunction')
var isSameType = require('../core/isSameType')
var constant = function (x) { return function () { return x; }; }
var applyTransform = function (left, maybe) { return maybe.either(
constant(Async.Rejected(left)),
Async.Resolved
); }
// maybeToAsync : e -> Maybe a -> Async e a
// maybeToAsync : e -> (a -> Maybe b) -> a -> Async e b
function maybeToAsync(left, maybe) {
if(isFunction(maybe)) {
return function(x) {
var m = maybe(x)
if(!isSameType(Maybe, m)) {
throw new TypeError('maybeToAsync: Maybe returning function required for second argument')
}
return applyTransform(left, m)
}
}
if(isSameType(Maybe, maybe)) {
return applyTransform(left, maybe)
}
throw new TypeError('maybeToAsync: Maybe or Maybe returning function required for second argument')
}
module.exports = curry(maybeToAsync)