forked from gcanti/tcomb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.js
136 lines (109 loc) · 4.41 KB
/
func.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var assert = require('./assert');
var isTypeName = require('./isTypeName');
var FunctionType = require('./Function');
var isArray = require('./isArray');
var list = require('./list');
var isObject = require('./isObject');
var create = require('./create');
var isNil = require('./isNil');
var isBoolean = require('./isBoolean');
var tuple = require('./tuple');
var getFunctionName = require('./getFunctionName');
var getTypeName = require('./getTypeName');
var isType = require('./isType');
function getDefaultName(domain, codomain) {
return '(' + domain.map(getTypeName).join(', ') + ') => ' + getTypeName(codomain);
}
function isInstrumented(f) {
return FunctionType.is(f) && isObject(f.instrumentation);
}
function getOptionalArgumentsIndex(types) {
var end = types.length;
var areAllMaybes = false;
for (var i = end - 1; i >= 0; i--) {
var type = types[i];
if (!isType(type) || type.meta.kind !== 'maybe') {
return (i + 1);
} else {
areAllMaybes = true;
}
}
return areAllMaybes ? 0 : end;
}
function func(domain, codomain, name) {
domain = isArray(domain) ? domain : [domain]; // handle handy syntax for unary functions
if (process.env.NODE_ENV !== 'production') {
assert(list(FunctionType).is(domain), function () { return 'Invalid argument domain ' + assert.stringify(domain) + ' supplied to func(domain, codomain, [name]) combinator (expected an array of types)'; });
assert(FunctionType.is(codomain), function () { return 'Invalid argument codomain ' + assert.stringify(codomain) + ' supplied to func(domain, codomain, [name]) combinator (expected a type)'; });
assert(isTypeName(name), function () { return 'Invalid argument name ' + assert.stringify(name) + ' supplied to func(domain, codomain, [name]) combinator (expected a string)'; });
}
var displayName = name || getDefaultName(domain, codomain);
var domainLength = domain.length;
var optionalArgumentsIndex = getOptionalArgumentsIndex(domain);
function FuncType(value, path) {
if (!isInstrumented(value)) { // automatically instrument the function
return FuncType.of(value);
}
if (process.env.NODE_ENV !== 'production') {
path = path || [displayName];
assert(FuncType.is(value), function () { return 'Invalid value ' + assert.stringify(value) + ' supplied to ' + path.join('/'); });
}
return value;
}
FuncType.meta = {
kind: 'func',
domain: domain,
codomain: codomain,
name: name,
identity: true
};
FuncType.displayName = displayName;
FuncType.is = function (x) {
return isInstrumented(x) &&
x.instrumentation.domain.length === domainLength &&
x.instrumentation.domain.every(function (type, i) {
return type === domain[i];
}) &&
x.instrumentation.codomain === codomain;
};
FuncType.of = function (f, curried) {
if (process.env.NODE_ENV !== 'production') {
assert(FunctionType.is(f), function () { return 'Invalid argument f supplied to func.of ' + displayName + ' (expected a function)'; });
assert(isNil(curried) || isBoolean(curried), function () { return 'Invalid argument curried ' + assert.stringify(curried) + ' supplied to func.of ' + displayName + ' (expected a boolean)'; });
}
if (FuncType.is(f)) { // makes FuncType.of idempotent
return f;
}
function fn() {
var args = Array.prototype.slice.call(arguments);
var argsLength = args.length;
if (process.env.NODE_ENV !== 'production') {
// type-check arguments
var tupleLength = curried ? argsLength : Math.max(argsLength, optionalArgumentsIndex);
tuple(domain.slice(0, tupleLength), 'arguments of function ' + displayName)(args);
}
if (curried && argsLength < domainLength) {
if (process.env.NODE_ENV !== 'production') {
assert(argsLength > 0, 'Invalid arguments.length = 0 for curried function ' + displayName);
}
var g = Function.prototype.bind.apply(f, [this].concat(args));
var newDomain = func(domain.slice(argsLength), codomain);
return newDomain.of(g, true);
}
else {
return create(codomain, f.apply(this, args));
}
}
fn.instrumentation = {
domain: domain,
codomain: codomain,
f: f
};
fn.displayName = getFunctionName(f);
return fn;
};
return FuncType;
}
func.getDefaultName = getDefaultName;
func.getOptionalArgumentsIndex = getOptionalArgumentsIndex;
module.exports = func;