crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
98 lines (73 loc) • 2.06 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 type = require('./types').type('Unit')
var _type = require('./types').typeFn(type(), VERSION)
var fl = require('./flNames')
var isFunction = require('./isFunction')
var isSameType = require('./isSameType')
var _of =
Unit
var _empty =
Unit
function Unit() {
var obj;
var equals =
function (m) { return isSameType(Unit, m); }
var inspect =
function () { return '()'; }
var valueOf =
function () { return undefined; }
var of =
_of
var empty =
_empty
function concat(method) {
return function(m) {
if(!isSameType(Unit, m)) {
throw new TypeError(("Unit." + method + ": Unit required"))
}
return Unit()
}
}
function map(method) {
return function(fn) {
if(!isFunction(fn)) {
throw new TypeError(("Unit." + method + ": Function required"))
}
return Unit()
}
}
function ap(m) {
if(!isSameType(Unit, m)) {
throw new TypeError('Unit.ap: Unit required')
}
return Unit()
}
function chain(method) {
return function(fn) {
if(!isFunction(fn)) {
throw new TypeError(("Unit." + method + ": Function required"))
}
return Unit()
}
}
return ( obj = {
inspect: inspect, toString: inspect, valueOf: valueOf,
type: type, equals: equals, empty: empty, ap: ap, of: of,
concat: concat('concat'),
map: map('map'),
chain: chain('chain')
}, obj[fl.of] = of, obj[fl.empty] = empty, obj[fl.equals] = equals, obj[fl.concat] = concat(fl.concat), obj[fl.map] = map(fl.map), obj[fl.chain] = chain(fl.chain), obj['@@type'] = _type, obj.constructor = Unit, obj )
}
Unit.of = _of
Unit.empty = _empty
Unit.type = type
Unit[fl.of] = _of
Unit[fl.empty] = _empty
Unit['@@type'] = _type
Unit['@@implements'] = _implements(
[ 'ap', 'chain', 'concat', 'empty', 'equals', 'map', 'of' ]
)
module.exports = Unit