-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
222 lines (189 loc) · 4.92 KB
/
index.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// deep equals
const isObject = (obj) => typeof obj === "object" && obj !== null;
// TODO does this work on arrays?
const deepEqual = (obj1, obj2) => {
if (obj1 === obj2) {
return true;
}
if (isObject(obj1) && isObject(obj2)) {
if (Object.keys(obj1).length !== Object.keys(obj2).length) {
return false;
}
for (var prop in obj1) {
if (!deepEqual(obj1[prop], obj2[prop])) {
return false;
}
}
return true;
}
return false;
}
const curryToArity = (fn, arity) => {
const resolver = (...args) => {
return (...innerArgs) => {
// make sure the function won't return functions endlessly when called with no arguments
const newArgs = arity !== 0 && innerArgs.length === 0 ? [undefined] : innerArgs;
const allArgs = [...args, ...newArgs];
return allArgs.length >= arity ? fn(...allArgs) : resolver(...allArgs);
};
};
return resolver();
};
const append = (list, val) => [...list, val];
// Union
const unionEquals = function (union = {}) {
return (
this._ctor === union._ctor &&
deepEqual(this._args, union._args)
);
};
const union = props => {
const Union = function (ctor, args) {
this._ctor = ctor;
this._args = args;
return this;
};
// static
const tags = Object.keys(props);
Union._tags = tags;
tags.forEach(ctor => {
const arity = props[ctor];
Union[ctor] = curryToArity((...args) => new Union(ctor, args), arity);
});
// prototype
Union.prototype.equals = unionEquals;
Union.prototype.__UNION__ = true;
return Union;
};
// Pattern matching
// TODO handle default case
const matchWith = (union, cases) => {
const {_ctor: ctor} = union;
const allIncluded = (arr1, arr2) => {
return arr1.every(key => arr2.includes(key));
};
if (!union.__UNION__) {
throw new Error('Trying to pattern match a non-union type.');
}
if (!allIncluded(Object.keys(cases), union.constructor._tags)) {
throw new Error('There are not enough branches for all possibilities.');
}
if (!allIncluded(union.constructor._tags, Object.keys(cases))) {
throw new Error('There are unrecognized patters in some branches.');
}
if (!(ctor in cases) || typeof cases[ctor] !== 'function') {
throw new Error(`The constructor ${ctor} is not a function.`);
}
return cases[ctor](...union._args);
};
// Maybe
const Maybe = union({
Just: 1,
Nothing: 0,
});
Maybe.of = Maybe.Just;
Maybe.prototype.map = function (func) {
return matchWith(this, {
Just: val => Maybe.Just(func(val)),
Nothing: () => this,
});
};
Maybe.prototype.chain = function (func) {
return matchWith(this, {
Just: func,
Nothing: () => this,
});
};
Maybe.prototype.ap = function (maybeVal) {
return matchWith(this, {
Just: func => maybeVal.map(func),
Nothing: () => this,
});
};
// Result
const Result = union({
Err: 1,
Ok: 1,
});
Result.of = Result.Ok;
Result.prototype.map = function (func) {
return matchWith(this, {
Err: () => this,
Ok: val => Result.Ok(func(val)),
});
};
Result.prototype.mapError = function (func) {
return matchWith(this, {
Err: val => Result.Err(func(val)),
Ok: () => this,
});
};
Result.prototype.chain = function (func) {
return matchWith(this, {
Err: () => this,
Ok: func,
});
};
Result.prototype.ap = function (resultVal) {
return matchWith(this, {
Err: () => this,
Ok: (func) => resultVal.map(func),
});
};
// Task
const Task = function (f, args = []) {
this._func = f;
this._args = args;
};
Task.of = (...args) => {
return new Task(...args);
};
Task.succeed = val => {
return new Task(() => Promise.resolve(val));
};
Task.fail = err => {
return new Task(() => Promise.reject(err));
};
// Batching Tasks
// concurrently
Task.all = taskList => {
return new Task(() => {
const promises = taskList.map(task => task.run());
return Promise.all(promises);
});
};
// sequentially (convenience wrapper for a chained task)
Task.sequence = taskList =>
taskList.reduce((acc, task) => acc.map2(append, task), Task.succeed([]));
// should never be called manually (only by the calling program)
Task.prototype.run = function () {
return Promise.resolve(this._func(...this._args));
};
Task.prototype.map = function (func) {
const newFunc = () => this.run().then(func);
return new Task(newFunc);
};
Task.prototype.map2 = function (func, task) {
const newFunc = () =>
this.run().then(val1 => task.run().then(val2 => func(val1, val2)));
return new Task(newFunc);
};
Task.prototype.mapError = function (func) {
const newFunc = () => this.run().catch(err => Promise.reject(func(err)));
return new Task(newFunc);
};
Task.prototype.chain = function (func) {
const newFunc = () => this.run().then(val => func(val).run());
return new Task(newFunc);
};
Task.prototype.onError = function (func) {
const newFunc = () => this.run().catch(err => func(err).run());
return new Task(newFunc);
};
export {
union,
matchWith,
Maybe,
Result,
Task,
};