crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
43 lines (31 loc) • 961 B
JavaScript
/** @license ISC License (c) copyright 2019 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
var List = 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 maybe.either(
List.empty,
List.of
); }
var err =
'maybeToList: Argument must be a Maybe instanstace or a Maybe returning function'
// maybeToList : Maybe a -> List a
// maybeToList : (a -> Maybe b) -> a -> List b
function maybeToList(maybe) {
if(isFunction(maybe)) {
return function(x) {
var m = maybe(x)
if(!isSameType(Maybe, m)) {
throw new TypeError(err)
}
return applyTransform(m)
}
}
if(isSameType(Maybe, maybe)) {
return applyTransform(maybe)
}
throw new TypeError(err)
}
module.exports = curry(maybeToList)