crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
77 lines (57 loc) • 1.88 kB
JavaScript
/** @license ISC License (c) copyright 2016 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
var VERSION = 2
var _implements = require('./implements')
var _inspect = require('./inspect')
var type = require('./types').type('Pred')
var _type = require('./types').typeFn(type(), VERSION)
var fl = require('./flNames')
var compose = require('./compose')
var isFunction = require('./isFunction')
var isSameType = require('./isSameType')
var _empty =
function () { return Pred(function () { return true; }); }
function Pred(pred) {
var obj;
if(!isFunction(pred)) {
throw new TypeError('Pred: Predicate function required')
}
var runWith =
function (x) { return !!pred(x); }
var inspect =
function () { return ("Pred" + (_inspect(runWith))); }
var empty =
_empty
var valueOf =
function () { return runWith; }
function concat(method) {
return function(m) {
if(!isSameType(Pred, m)) {
throw new TypeError(("Pred." + method + ": Pred required"))
}
return Pred(function (x) { return !!runWith(x) && !!m.runWith(x); })
}
}
function contramap(method) {
return function(fn) {
if(!isFunction(fn)) {
throw new TypeError(("Pred." + method + ": Function required"))
}
return Pred(compose(runWith, fn))
}
}
return ( obj = {
inspect: inspect, toString: inspect,
runWith: runWith, type: type, valueOf: valueOf, empty: empty,
concat: concat('concat'),
contramap: contramap('contramap')
}, obj[fl.empty] = empty, obj[fl.concat] = concat(fl.concat), obj[fl.contramap] = contramap(fl.contramap), obj['@@type'] = _type, obj.constructor = Pred, obj )
}
Pred.empty = _empty
Pred.type = type
Pred[fl.empty] = _empty
Pred['@@type'] = _type
Pred['@@implements'] = _implements(
[ 'concat', 'contramap', 'empty' ]
)
module.exports = Pred