This repository has been archived by the owner on Nov 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
adt.js
540 lines (452 loc) · 14.6 KB
/
adt.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// adt.js
// ------
// Algebraic data types and immutable structures in Javascript
//
// version : 0.7.2
// author : Nathan Faubion <nathan@n-son.com>
// license : MIT
;(function (adt) {
'use strict';
// Base class from which all adt.js classes inherit.
adt.__Base__ = function () {};
// ADT Class Generation
// --------------------
adt.data = function () {
var targ0 = typeof arguments[0];
// adt.data(...names: String)
if (targ0 === 'string') {
var names = arguments;
return adt.data(function (type) {
var i = 0, len = names.length;
for (; i < len; i++) type(names[i]);
});
}
// adt.data(types: Object)
if (targ0 === 'object') {
var types = arguments[0];
return adt.data(function (type) {
for (var name in types) {
if (types.hasOwnProperty(name)) type(name, types[name]);
}
});
}
// adt.data(configure: Function)
var callback = arguments[0] || noop;
var names = [];
// Create a new parent class.
// This class should never be created using `new`. You obviously can,
// but it won't be of much use. You can however override the apply method
// to create default instances.
var D = inherit(adt.__Base__, function () {
if (!(this instanceof D) && D.apply !== Function.prototype.apply) {
return D.apply(this, arguments);
}
throw new Error('Bad invocation');
});
// Adds a new type to the ADT.
D.type = function (name, tmpl) {
if (typeof name !== 'string') {
tmpl = name;
name = uniqueId('Anonymous');
}
// Create a new template if not provided with one
var isSingle = checkTypes([String, Boolean, Number, Date, null, void 0], tmpl);
if (isSingle) tmpl = adt.single(tmpl);
else if (typeof tmpl !== 'function') {
tmpl = checkType(Array, tmpl)
? adt.record.apply(null, tmpl)
: adt.record(tmpl);
}
// Add typechecking attributes for this type. Everything starts out as
// false by default. Each individual class should overrides its own.
D.prototype['is' + name] = false;
// Call the template to build our type.
var d = tmpl(D, name);
// Bind the constructor context to avoid conflicts with calling as a method.
d = (typeof d === 'function') ? extend(d.bind(), d) : d;
// Export it on the parent type.
D[name] = d;
names.push(name);
return d;
};
// Call the callback with the constructor as the context.
var types = callback.call(D, D.type, D);
// If an object was returned in the callback, assume it's a mapping of
// more types to add.
if (typeof types === 'object' && !(types instanceof adt.__Base__)) {
for (var name in types) {
if (types.hasOwnProperty(name)) D.type(name, types[name]);
}
}
// Keep the type function around because it allows for nice type
// declarations, but give the option to seal it. This will call `seal`
// on any sub types to.
D.seal = function () {
var i = 0, n, name;
for (; n = names[i]; i++) if (this[n].seal) this[n].seal();
delete D.type;
delete D.seal;
return D;
};
// Export names as a meta object
D.__names__ = names;
D.prototype.__adtClass__ = D;
return D;
};
// Singleton Class Generation
// --------------------------
// Create a single empty class instance. You can pass in a value that the
// class will use during JSON serialization.
adt.single = function (val) {
var ctr = function () {};
ctr.__value__ = val === void 0 ? null : val;
return function (parent, name) {
inherit(parent, ctr);
extend(ctr.prototype, adt.single.__methods__);
ctr.className = name;
ctr.prototype['is' + name] = true;
return new ctr();
};
}
// Singleton Methods
// -----------------
adt.single.__methods__ = {
toString: function () {
return this.constructor.className;
},
toJSON: function () {
return this.constructor.__value__;
},
clone: function () {
return this;
},
equals: function (that) {
return this === that;
},
hasInstance: function(that) {
return this === that;
}
};
// Record Class Generation
// -----------------------
adt.record = function () {
var targ0 = typeof arguments[0];
// adt.record(...names: String)
if (targ0 === 'string') {
var names = arguments;
return adt.record(function (field) {
var i = 0, len = names.length;
for (; i < len; i++) field(names[i], adt.any);
});
}
// adt.record(fields: Object)
else if (targ0 === 'object') {
var fields = arguments[0];
return adt.record(function (field) {
for (var name in fields) {
if (fields.hasOwnProperty(name)) field(name, fields[name]);
}
});
}
// adt.record(template: Function)
var callback = arguments[0] || noop;
var names = [];
var constraints = {};
// A record's constructor can be called without `new` and will also throw
// an error if called with the wrong number of arguments. Its arguments can
// be curried as long as it isn't called with the `new` keyword.
var ctr = function () {
var args = arguments;
var len = names.length;
if (this instanceof ctr) {
if (args.length !== len) {
throw new Error(
'Unexpected number of arguments for ' + ctr.className + ': ' +
'got ' + args.length + ', but need ' + len + '.'
);
}
var i = 0, n;
for (; n = names[i]; i++) {
this[n] = constraints[n](args[i], n, ctr);
}
} else {
return args.length < len
? partial(ctr, toArray(args))
: ctrApply(ctr, args);
}
};
return function (parent, name) {
inherit(parent, ctr);
extend(ctr, adt.record.__classMethods__);
extend(ctr.prototype, adt.record.__methods__);
ctr.className = name;
ctr.prototype['is' + name] = true;
// Declares a field as part of the type.
ctr.field = function (name, constraint) {
if (!constraint) constraint = adt.any;
if (typeof constraint !== 'function') {
throw new TypeError('Constraints must be functions')
}
names.push(name);
constraints[name] = constraint;
return ctr;
};
// Call the callback with the contructor as the context.
var fields = callback.call(ctr, ctr.field, ctr);
// If an object was returned in the callback, assume it's a mapping of
// more fields to add.
if (typeof fields === 'object' && fields !== ctr) {
for (var name in fields) {
if (fields.hasOwnProperty(name)) ctr.field(name, fields[name]);
}
}
// Export names and constraints as meta attributes.
ctr.__names__ = names;
ctr.__constraints__ = constraints;
return ctr;
};
};
// Record Methods
// --------------
adt.record.__methods__ = {
toString: function () {
var ctr = this.constructor;
var vals = ctr.unapply(this);
return ctr.className + (vals.length ? '(' + vals.join(', ') + ')' : '');
},
toJSON: function () {
return this.constructor.unapplyObject(this, toJSONValue);
},
// Clones any value that is an adt.js type, delegating other JS values
// to `adt.nativeClone`.
clone: function () {
var ctr = this.constructor;
var names = ctr.__names__;
var args = [], i = 0, n, val;
for (; n = names[i]; i++) {
val = this[n];
args[i] = val instanceof adt.__Base__
? val.clone()
: adt.nativeClone(val);
}
return ctr.apply(null, args);
},
// Recursively compares all adt.js types, delegating other JS values
// to `adt.nativeEquals`.
equals: function (that) {
var ctr = this.constructor;
if (this === that) return true;
if (!(that instanceof ctr)) return false;
var names = ctr.__names__;
var i = 0, len = names.length;
var vala, valb, n;
for (; i < len; i++) {
n = names[i], vala = this[n], valb = that[n];
if (vala instanceof adt.__Base__) {
if (!vala.equals(valb)) return false;
} else if (!adt.nativeEquals(vala, valb)) return false;
}
return true;
},
// Overloaded to take either strings or numbers. Throws an error if the
// key can't be found.
get: function (field) {
var ctr = this.constructor;
var names = ctr.__names__;
var constraints = ctr.__constraints__;
if (typeof field === 'number') {
if (field < 0 || field > names.length - 1) {
throw new Error('Field index out of range: ' + field);
}
field = names[field];
} else {
if (!constraints.hasOwnProperty(field)) {
throw new Error('Field name does not exist: ' + field);
}
}
return this[field];
},
set: function (vals) {
var ctr = this.constructor;
var names = ctr.__names__;
var args = [], i = 0, n;
for (; n = names[i]; i++) args[i] = n in vals ? vals[n] : this[n];
return ctr.apply(null, args);
}
};
adt.record.__classMethods__ = {
create: function (vals) {
var args = [];
var names = this.__names__;
var i = 0, len = names.length, n;
for (; n = names[i]; i++) {
if (!(n in vals)) {
throw new Error('Missing `' + n + '` in arguments to ' + this.className);
}
args[i] = vals[n];
}
return this.apply(null, args);
},
hasInstance: function (inst) {
return inst instanceof this;
},
unapply: function (inst, fn) {
if (this.hasInstance(inst)) {
var names = this.__names__;
var vals = [], i = 0, n;
for (; n = names[i]; i++) vals[i] = fn ? fn(inst[n], n) : inst[n];
return vals;
}
},
unapplyObject: function (inst, fn) {
if (this.hasInstance(inst)) {
var names = this.__names__;
var vals = {}, i = 0, n;
for (; n = names[i]; i++) vals[n] = fn ? fn(inst[n], n) : inst[n];
return vals;
}
},
seal: function () {
delete this.field;
delete this.seal;
return this;
}
};
// Enum Class Generation
// ---------------------
adt.enumeration = function () {
var E = adt.data.apply(null, arguments);
var order = 0;
// Helper to add the order meta attribute to a type.
function addOrder (that) {
if (that.constructor) that = that.constructor;
that.__order__ = order++;
return that;
}
// Iterate through the created types, applying the order meta attribute.
for (var i = 0, n; n = E.__names__[i]; i++) addOrder(E[n]);
// Patch the type function to add an order to any types created later.
var __type = E.type;
E.type = function () {
return addOrder(__type.apply(E, arguments));
};
extend(E.prototype, adt.enumeration.__methods__);
return E;
};
adt['enum'] = adt.enumeration;
// Enum Methods
// ------------
function assertADT (a, b) {
if (b instanceof a.__adtClass__) return true;
throw new TypeError('Unexpected type');
}
function orderOf (that) {
return that.constructor.__order__;
}
adt.enumeration.__methods__ = {
lt: function (that) {
return assertADT(this, that) && orderOf(this) < orderOf(that);
},
lte: function (that) {
return assertADT(this, that) && orderOf(this) <= orderOf(that);
},
gt: function (that) {
return assertADT(this, that) && orderOf(this) > orderOf(that);
},
gte: function (that) {
return assertADT(this, that) && orderOf(this) >= orderOf(that);
},
eq: function (that) {
return assertADT(this, that) && orderOf(this) === orderOf(that);
},
neq: function (that) {
return assertADT(this, that) && orderOf(this) !== orderOf(that);
},
};
// Public Helpers
// --------------
// Cloning for native JS types just returns a reference.
adt.nativeClone = function (x) { return x; };
// Equality for native JS types is just strict comparison.
adt.nativeEquals = function (a, b) { return a === b; };
// Shortcut for creating an ADT with only one type.
adt.newtype = function () {
var args = toArray(arguments);
var data = adt.data();
return data.type.apply(data, args);
};
// A contraint function that will accept any value.
adt.any = function (x) { return x; };
// A constraint generator that will perform instanceof checks on the value
// to make sure it is of the correct type.
adt.only = function () {
var args = arguments;
return function (x, field, ctr) {
if (checkTypes(args, x)) return x;
var err = 'Unexpected type';
if (field && ctr) err += ' for `' + field + '` of ' + ctr.className;
throw new TypeError(err);
};
};
// Utility Functions
// -----------------
function toArray (a, start) {
var dest = [], i = start || 0, len = a.length;
for (; i < len; i++) dest.push(a[i]);
return dest;
}
function ctrApply (ctr, args) {
var C = function () {};
C.prototype = ctr.prototype;
var inst = new C();
var ret = ctr.apply(inst, args);
return inst;
}
function inherit (sup, sub) {
var C = function () {};
C.prototype = sup.prototype;
sub.prototype = new C();
sub.prototype.constructor = sub;
return sub;
}
function partial (func, args) {
return function () {
return func.apply(this, args.concat(toArray(arguments)));
};
}
function extend (dest /*, ...sources*/) {
var args = toArray(arguments, 1);
var i = 0, len = args.length, k;
for (; i < len; i++) {
for (k in args[i]) {
if (args[i].hasOwnProperty(k)) dest[k] = args[i][k];
}
}
return dest;
};
function checkType (type, x) {
if (type instanceof Function) {
if (x instanceof type
|| type === Number && typeof x === 'number'
|| type === String && typeof x === 'string'
|| type === Boolean && typeof x === 'boolean') return true;
} else {
if (type instanceof adt.__Base__ && type.equals(x)
|| type === x) return true;
}
return false;
}
function checkTypes(types, x) {
var i = 0, len = types.length;
for (; i < len; i++) if (checkType(types[i], x)) return true;
return false;
}
function toJSONValue (x) {
return x && typeof x === 'object' && x.toJSON ? x.toJSON() : x;
}
var id = 0;
function uniqueId (pre) {
return (pre || '') + id++;
}
function noop () {}
})(typeof exports !== 'undefined' ? exports : (this.adt = {}));