crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
36 lines (26 loc) • 940 B
JavaScript
/** @license ISC License (c) copyright 2017 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
var First = require('.')
var Maybe = require('../core/types').proxy('Maybe')
var curry = require('../core/curry')
var isFunction = require('../core/isFunction')
var isSameType = require('../core/isSameType')
var applyTransform = function (maybe) { return First(maybe); }
// maybeToFirst : Maybe a -> First a
// maybeToFirst : (a -> Maybe b) -> a -> First b
function maybeToFirst(maybe) {
if(isFunction(maybe)) {
return function(x) {
var m = maybe(x)
if(!isSameType(Maybe, m)) {
throw new TypeError('maybeToFirst: Maybe returning function required')
}
return applyTransform(m)
}
}
if(isSameType(Maybe, maybe)) {
return applyTransform(maybe)
}
throw new TypeError('maybeToFirst: Maybe or Maybe returning function required')
}
module.exports = curry(maybeToFirst)