forked from gcanti/tcomb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
896 lines (691 loc) · 24.8 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
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.t = factory();
}
}(this, function () {
'use strict';
var failed = false;
function onFail(message) {
// start debugger only once
if (!failed) {
/*
DEBUG HINT:
if you are reading this, chances are that there is a bug in your system
see the Call Stack to find out what's wrong..
*/
/*jshint debug: true*/
debugger;
}
failed = true;
throw new Error(message);
}
var options = {
onFail: onFail
};
function fail(message) {
/*
DEBUG HINT:
if you are reading this, chances are that there is a bug in your system
see the Call Stack to find out what's wrong..
*/
options.onFail(message);
}
function assert(guard) {
if (guard !== true) {
var args = slice.call(arguments, 1);
var message = args[0] ? format.apply(null, args) : 'assert failed';
/*
DEBUG HINT:
if you are reading this, chances are that there is a bug in your system
see the Call Stack to find out what's wrong..
*/
fail(message);
}
}
//
// utils
//
var slice = Array.prototype.slice;
function mixin(target, source, overwrite) {
if (Nil.is(source)) {
return target;
}
for (var k in source) {
if (source.hasOwnProperty(k)) {
if (overwrite !== true) {
assert(!target.hasOwnProperty(k), 'cannot overwrite property %s', k);
}
target[k] = source[k];
}
}
return target;
}
function format() {
var args = slice.call(arguments);
var len = args.length;
var i = 1;
var message = args[0];
function formatArgument(match, type) {
if (match === '%%') { return '%'; } // handle escaping %
if (i >= len) { return match; } // handle less arguments than placeholders
var formatter = format.formatters[type];
if (!formatter) { return match; } // handle undefined formatters
return formatter(args[i++]);
}
var str = message.replace(/%([a-z%])/g, formatArgument);
if (i < len) {
str += ' ' + args.slice(i).join(' '); // handle more arguments than placeholders
}
return str;
}
function replacer(key, value) {
if (typeof value === 'function') {
return format('Func', value.name);
}
return value;
}
format.formatters = {
s: function formatString(x) { return String(x); },
j: function formatJSON(x) { return JSON.stringify(x, replacer); }
};
function getName(type) {
assert(Type.is(type), 'Invalid argument `type` of value `%j` supplied to `getName()`, expected a type.', type);
return type.meta.name;
}
function getKind(type) {
assert(Type.is(type), 'Invalid argument `type` of value `%j` supplied to `geKind()`, expected a type.', type);
return type.meta.kind;
}
function blockNew(x, type) {
assert(!(x instanceof type), 'Operator `new` is forbidden for `%s`', getName(type));
}
function shallowCopy(x) {
return Arr.is(x) ? x.concat() : Obj.is(x) ? mixin({}, x) : x;
}
function update(instance, spec) {
assert(Obj.is(spec));
var value = shallowCopy(instance);
for (var k in spec) {
if (spec.hasOwnProperty(k)) {
if (update.commands.hasOwnProperty(k)) {
assert(Object.keys(spec).length === 1);
return update.commands[k](spec[k], value);
} else {
value[k] = update(value[k], spec[k]);
}
}
}
return value;
}
update.commands = {
'$apply': function $apply(f, value) {
assert(Func.is(f));
return f(value);
},
'$push': function $push(elements, arr) {
assert(Arr.is(elements));
assert(Arr.is(arr));
return arr.concat(elements);
},
'$remove': function $remove(keys, obj) {
assert(Arr.is(keys));
assert(Obj.is(obj));
for (var i = 0, len = keys.length ; i < len ; i++ ) {
delete obj[keys[i]];
}
return obj;
},
'$set': function $set(value) {
return value;
},
'$splice': function $splice(splices, arr) {
assert(list(Arr).is(splices));
assert(Arr.is(arr));
return splices.reduce(function reducer(acc, splice) {
acc.splice.apply(acc, splice);
return acc;
}, arr);
},
'$swap': function $swap(config, arr) {
assert(Obj.is(config));
assert(Num.is(config.from));
assert(Num.is(config.to));
assert(Arr.is(arr));
var element = arr[config.to];
arr[config.to] = arr[config.from];
arr[config.from] = element;
return arr;
},
'$unshift': function $unshift(elements, arr) {
assert(Arr.is(elements));
assert(Arr.is(arr));
return elements.concat(arr);
}
};
//
// irriducibles
//
function irriducible(name, is) {
// DEBUG HINT: if the debugger stops here, the first argument is not a string
assert(typeof name === 'string', 'Invalid argument `name` supplied to `irriducible()`');
// DEBUG HINT: if the debugger stops here, the second argument is not a function
assert(typeof is === 'function', 'Invalid argument `is` supplied to `irriducible()`');
function Irriducible(value) {
// DEBUG HINT: if the debugger stops here, you have used the `new` operator but it's forbidden
blockNew(this, Irriducible);
// DEBUG HINT: if the debugger stops here, the first argument is invalid
// mouse over the `value` variable to see what's wrong. In `name` there is the name of the type
assert(is(value), 'Invalid `%s` supplied to `%s`', value, name);
return value;
}
Irriducible.meta = {
kind: 'irriducible',
name: name
};
Irriducible.displayName = name;
Irriducible.is = is;
return Irriducible;
}
var Any = irriducible('Any', function isAny() {
return true;
});
var Nil = irriducible('Nil', function isNil(x) {
return x === null || x === void 0;
});
var Str = irriducible('Str', function isStr(x) {
return typeof x === 'string';
});
var Num = irriducible('Num', function isNum(x) {
return typeof x === 'number' && isFinite(x) && !isNaN(x);
});
var Bool = irriducible('Bool', function isBool(x) {
return x === true || x === false;
});
var Arr = irriducible('Arr', function isArr(x) {
return x instanceof Array;
});
var Obj = irriducible('Obj', function isObj(x) {
return !Nil.is(x) && typeof x === 'object' && !Arr.is(x);
});
var Func = irriducible('Func', function isFunc(x) {
return typeof x === 'function';
});
var Err = irriducible('Err', function isErr(x) {
return x instanceof Error;
});
var Re = irriducible('Re', function isRe(x) {
return x instanceof RegExp;
});
var Dat = irriducible('Dat', function isDat(x) {
return x instanceof Date;
});
var Type = irriducible('Type', function isType(x) {
return Func.is(x) && Obj.is(x.meta);
});
function struct(props, name) {
// DEBUG HINT: if the debugger stops here, the first argument is not a dict of types
// mouse over the `props` variable to see what's wrong
assert(dict(Str, Type).is(props), 'Invalid argument `props` supplied to `struct()`');
// DEBUG HINT: if the debugger stops here, the second argument is not a string
// mouse over the `name` variable to see what's wrong
assert(maybe(Str).is(name), 'Invalid argument `name` supplied to `struct()`');
// DEBUG HINT: always give a name to a type, the debug will be easier
name = name || 'struct';
function Struct(value, mut) {
// makes Struct idempotent
if (Struct.is(value)) {
return value;
}
// DEBUG HINT: if the debugger stops here, the first argument is invalid
// mouse over the `value` variable to see what's wrong. In `name` there is the name of the type
assert(Obj.is(value), 'Invalid `%s` supplied to `%s`, expected an `Obj`', value, name);
// makes `new` optional
if (!(this instanceof Struct)) {
return new Struct(value, mut);
}
for (var k in props) {
if (props.hasOwnProperty(k)) {
var expected = props[k];
var actual = value[k];
// DEBUG HINT: if the debugger stops here, the `actual` value supplied to the `expected` type is invalid
// mouse over the `actual` and `expected` variables to see what's wrong
this[k] = expected(actual, mut);
}
}
if (mut !== true) {
Object.freeze(this);
}
}
Struct.meta = {
kind: 'struct',
props: props,
name: name
};
Struct.displayName = name;
Struct.is = function isStruct(x) {
return x instanceof Struct;
};
Struct.update = function updateStruct(instance, spec, value) {
return new Struct(update(instance, spec, value));
};
Struct.extend = function extendStruct(newProps, name) {
return struct([props].concat(newProps).reduce(mixin, {}), name);
};
return Struct;
}
function union(types, name) {
// DEBUG HINT: if the debugger stops here, the first argument is not a list of types
assert(list(Type).is(types), 'Invalid argument `types` supplied to `union()`');
var len = types.length;
// DEBUG HINT: if the debugger stops here, there are too few types (they must be at least two)
assert(len >= 2, 'Invalid argument `types` supplied to `union()`');
// DEBUG HINT: if the debugger stops here, the second argument is not a string
// mouse over the `name` variable to see what's wrong
assert(maybe(Str).is(name), 'Invalid argument `name` supplied to `union()`');
name = name || format('union([%s])', types.map(getName).join(', '));
function Union(value, mut) {
// DEBUG HINT: if the debugger stops here, you have used the `new` operator but it's forbidden
blockNew(this, Union);
// DEBUG HINT: if the debugger stops here, you must implement the `dispatch` static method for this type
assert(Func.is(Union.dispatch), 'unimplemented %s.dispatch()', name);
var type = Union.dispatch(value);
// DEBUG HINT: if the debugger stops here, the `dispatch` static method returns no type
assert(Type.is(type), '%s.dispatch() returns no type', name);
// DEBUG HINT: if the debugger stops here, `value` can't be converted to `type`
// mouse over the `value` and `type` variables to see what's wrong
return type(value, mut);
}
Union.meta = {
kind: 'union',
types: types,
name: name
};
Union.displayName = name;
Union.is = function isUnion(x) {
return types.some(function isType(type) {
return type.is(x);
});
};
// default dispatch implementation
Union.dispatch = function dispatch(x) {
for (var i = 0, len = types.length ; i < len ; i++ ) {
if (types[i].is(x)) {
return types[i];
}
}
};
return Union;
}
function maybe(type, name) {
// DEBUG HINT: if the debugger stops here, the first argument is not a type
assert(Type.is(type), 'Invalid argument `type` supplied to `maybe()`');
// makes the combinator idempotent
if (getKind(type) === 'maybe') {
return type;
}
// DEBUG HINT: if the debugger stops here, the second argument is not a string
// mouse over the `name` variable to see what's wrong
assert(Nil.is(name) || Str.is(name), 'Invalid argument `name` supplied to `maybe()`');
name = name || format('maybe(%s)', getName(type));
function Maybe(value, mut) {
// DEBUG HINT: if the debugger stops here, you have used the `new` operator but it's forbidden
blockNew(this, Maybe);
// DEBUG HINT: if the debugger stops here, `value` can't be converted to `type`
// mouse over the `value` and `type` variables to see what's wrong
return Nil.is(value) ? null : type(value, mut);
}
Maybe.meta = {
kind: 'maybe',
type: type,
name: name
};
Maybe.displayName = name;
Maybe.is = function isMaybe(x) {
return Nil.is(x) || type.is(x);
};
return Maybe;
}
function enums(map, name) {
// DEBUG HINT: if the debugger stops here, the first argument is not a hash
// mouse over the `map` variable to see what's wrong
assert(Obj.is(map), 'Invalid argument `map` supplied to `enums()`');
// DEBUG HINT: if the debugger stops here, the second argument is not a string
// mouse over the `name` variable to see what's wrong
assert(maybe(Str).is(name), 'Invalid argument `name` supplied to `enums()`');
name = name || 'enums';
// cache enums
var keys = Object.keys(map);
function Enums(value) {
// DEBUG HINT: if the debugger stops here, you have used the `new` operator but it's forbidden
blockNew(this, Enums);
// DEBUG HINT: if the debugger stops here, the value is not one of the defined enums
// mouse over the `value`, `name` and `keys` variables to see what's wrong
assert(Enums.is(value), 'Invalid `%s` supplied to `%s`, expected one of %j', value, name, keys);
return value;
}
Enums.meta = {
kind: 'enums',
map: map,
name: name
};
Enums.displayName = name;
Enums.is = function isEnums(x) {
return Str.is(x) && map.hasOwnProperty(x);
};
return Enums;
}
enums.of = function enumsOf(keys, name) {
keys = Str.is(keys) ? keys.split(' ') : keys;
var value = {};
keys.forEach(function setEnum(k) {
value[k] = k;
});
return enums(value, name);
};
function tuple(types, name) {
// DEBUG HINT: if the debugger stops here, the first argument is not a list of types
assert(list(Type).is(types), 'Invalid argument `types` supplied to `tuple()`');
var len = types.length;
// DEBUG HINT: if the debugger stops here, the second argument is not a string
// mouse over the `name` variable to see what's wrong
assert(maybe(Str).is(name), 'Invalid argument `name` supplied to `tuple()`');
name = name || format('tuple([%s])', types.map(getName).join(', '));
function Tuple(value, mut) {
// DEBUG HINT: if the debugger stops here, the value is not one of the defined enums
// mouse over the `value`, `name` and `len` variables to see what's wrong
assert(Arr.is(value) && value.length === len, 'Invalid `%s` supplied to `%s`, expected an `Arr` of length `%s`', value, name, len);
// makes Tuple idempotent
if (Tuple.isTuple(value)) {
return value;
}
var arr = [];
for (var i = 0 ; i < len ; i++) {
var expected = types[i];
var actual = value[i];
// DEBUG HINT: if the debugger stops here, the `actual` value supplied to the `expected` type is invalid
// mouse over the `actual` and `expected` variables to see what's wrong
arr.push(expected(actual, mut));
}
if (mut !== true) {
Object.freeze(arr);
}
return arr;
}
Tuple.meta = {
kind: 'tuple',
types: types,
length: len,
name: name
};
Tuple.displayName = name;
Tuple.isTuple = function isTuple(x) {
return types.every(function isType(type, i) {
return type.is(x[i]);
});
};
Tuple.is = function isTuple(x) {
return Arr.is(x) && x.length === len && Tuple.isTuple(x);
};
Tuple.update = function updateTuple(instance, spec, value) {
return Tuple(update(instance, spec, value));
};
return Tuple;
}
function subtype(type, predicate, name) {
// DEBUG HINT: if the debugger stops here, the first argument is not a type
assert(Type.is(type), 'Invalid argument `type` supplied to `subtype()`');
// DEBUG HINT: if the debugger stops here, the second argument is not a function
assert(Func.is(predicate), 'Invalid argument `predicate` supplied to `subtype()`');
// DEBUG HINT: if the debugger stops here, the third argument is not a string
// mouse over the `name` variable to see what's wrong
assert(maybe(Str).is(name), 'Invalid argument `name` supplied to `subtype()`');
// DEBUG HINT: always give a name to a type, the debug will be easier
name = name || format('subtype(%s)', getName(type));
// cache expected value
var expected = predicate.__doc__ || format('insert a valid value for %s', predicate.name || 'the subtype');
function Subtype(value, mut) {
// DEBUG HINT: if the debugger stops here, you have used the `new` operator but it's forbidden
blockNew(this, Subtype);
// DEBUG HINT: if the debugger stops here, the value cannot be converted to the base type
var x = type(value, mut);
// DEBUG HINT: if the debugger stops here, the value is converted to the base type
// but the predicate returns `false`
assert(predicate(x), 'Invalid `%s` supplied to `%s`, %s', value, name, expected);
return x;
}
Subtype.meta = {
kind: 'subtype',
type: type,
predicate: predicate,
name: name
};
Subtype.displayName = name;
Subtype.is = function isSubtype(x) {
return type.is(x) && predicate(x);
};
Subtype.update = function updateSubtype(instance, spec, value) {
return Subtype(update(instance, spec, value));
};
return Subtype;
}
function list(type, name) {
// DEBUG HINT: if the debugger stops here, the first argument is not a type
assert(Type.is(type), 'Invalid argument `type` supplied to `list()`');
// DEBUG HINT: if the debugger stops here, the third argument is not a string
// mouse over the `name` variable to see what's wrong
assert(maybe(Str).is(name), 'Invalid argument `name` supplied to `list()`');
// DEBUG HINT: always give a name to a type, the debug will be easier
name = name || format('list(%s)', getName(type));
function List(value, mut) {
// DEBUG HINT: if the debugger stops here, you have used the `new` operator but it's forbidden
// DEBUG HINT: if the debugger stops here, the value is not one of the defined enums
// mouse over the `value` and `name` variables to see what's wrong
assert(Arr.is(value), 'Invalid `%s` supplied to `%s`, expected an `Arr`', value, name);
// makes List idempotent
if (List.isList(value)) {
return value;
}
var arr = [];
for (var i = 0, len = value.length ; i < len ; i++ ) {
var actual = value[i];
// DEBUG HINT: if the debugger stops here, the `actual` value supplied to the `type` type is invalid
// mouse over the `actual` and `type` variables to see what's wrong
arr.push(type(actual, mut));
}
if (mut !== true) {
Object.freeze(arr);
}
return arr;
}
List.meta = {
kind: 'list',
type: type,
name: name
};
List.displayName = name;
List.isList = function isList(x) {
return x.every(type.is);
};
List.is = function isList(x) {
return Arr.is(x) && List.isList(x);
};
List.update = function updateList(instance, spec, value) {
return List(update(instance, spec, value));
};
return List;
}
function dict(domain, codomain, name) {
// DEBUG HINT: if the debugger stops here, the first argument is not a type
assert(Type.is(domain), 'Invalid argument `domain` supplied to `dict()`');
// DEBUG HINT: if the debugger stops here, the second argument is not a type
assert(Type.is(codomain), 'Invalid argument `codomain` supplied to `dict()`');
// DEBUG HINT: if the debugger stops here, the third argument is not a string
// mouse over the `name` variable to see what's wrong
assert(maybe(Str).is(name), 'Invalid argument `name` supplied to `dict()`');
// DEBUG HINT: always give a name to a type, the debug will be easier
name = name || format('dict(%s, %s)', getName(domain), getName(codomain));
function Dict(value, mut) {
// DEBUG HINT: if the debugger stops here, the value is not an object
// mouse over the `value` and `name` variables to see what's wrong
assert(Obj.is(value), 'Invalid `%s` supplied to `%s`, expected an `Obj`', value, name);
// makes Dict idempotent
if (Dict.isDict(value)) {
return value;
}
var obj = {};
for (var k in value) {
if (value.hasOwnProperty(k)) {
// DEBUG HINT: if the debugger stops here, the `k` value supplied to the `domain` type is invalid
// mouse over the `k` and `domain` variables to see what's wrong
k = domain(k);
var actual = value[k];
// DEBUG HINT: if the debugger stops here, the `actual` value supplied to the `codomain` type is invalid
// mouse over the `actual` and `codomain` variables to see what's wrong
obj[k] = codomain(actual, mut);
}
}
if (mut !== true) {
Object.freeze(obj);
}
return obj;
}
Dict.meta = {
kind: 'dict',
domain: domain,
codomain: codomain,
name: name
};
Dict.displayName = name;
Dict.isDict = function isDict(x) {
for (var k in x) {
if (x.hasOwnProperty(k)) {
if (!domain.is(k) || !codomain.is(x[k])) { return false; }
}
}
return true;
};
Dict.is = function isDict(x) {
return Obj.is(x) && Dict.isDict(x);
};
Dict.update = function updateDict(instance, spec, value) {
return Dict(update(instance, spec, value));
};
return Dict;
}
function func(domain, codomain, name) {
// handle handy syntax for unary functions
domain = Arr.is(domain) ? domain : [domain];
// DEBUG HINT: if the debugger stops here, the first argument is not a list of types
assert(list(Type).is(domain), 'Invalid argument `domain` supplied to `func()`');
// DEBUG HINT: if the debugger stops here, the second argument is not a type
assert(Type.is(codomain), 'Invalid argument `codomain` supplied to `func()`');
// DEBUG HINT: always give a name to a type, the debug will be easier
name = name || format('func([%s], %s)', domain.map(getName).join(', '), getName(codomain));
// cache the domain length
var domainLen = domain.length;
function Func(value) {
// automatically instrument the function if is not already instrumented
if (!func.is(value)) {
value = Func.of(value);
}
// DEBUG HINT: if the debugger stops here, the first argument is invalid
// mouse over the `value` and `name` variables to see what's wrong
assert(Func.is(value), 'Invalid `%s` supplied to `%s`', value, name);
return value;
}
Func.meta = {
kind: 'func',
domain: domain,
codomain: codomain,
name: name
};
Func.displayName = name;
Func.is = function isFunc(x) {
return func.is(x) &&
x.func.domain.length === domain.length &&
x.func.domain.every(function isEqual(type, i) {
return type === domain[i];
}) &&
x.func.codomain === codomain;
};
Func.of = function funcOf(f) {
// DEBUG HINT: if the debugger stops here, f is not a function
assert(typeof f === 'function');
// makes Func.of idempotent
if (Func.is(f)) {
return f;
}
function fn() {
var args = slice.call(arguments);
var len = Math.min(args.length, domainLen);
// DEBUG HINT: if the debugger stops here, you provided wrong arguments to the function
// mouse over the `args` variable to see what's wrong
args = tuple(domain.slice(0, len))(args);
if (len === domainLen) {
/* jshint validthis: true */
var r = f.apply(this, args);
// DEBUG HINT: if the debugger stops here, the return value of the function is invalid
// mouse over the `r` variable to see what's wrong
r = codomain(r);
return r;
} else {
var curried = Function.prototype.bind.apply(f, [this].concat(args));
var newdomain = func(domain.slice(len), codomain);
return newdomain.of(curried);
}
}
fn.func = {
domain: domain,
codomain: codomain,
f: f
};
return fn;
};
return Func;
}
// returns true if x is an instrumented function
func.is = function isFunc(f) {
return Func.is(f) && Obj.is(f.func);
};
return {
util: {
mixin: mixin,
format: format,
getName: getName,
getKind: getKind,
slice: slice,
shallowCopy: shallowCopy,
update: update
},
options: options,
assert: assert,
fail: fail,
Any: Any,
Nil: Nil,
Str: Str,
Num: Num,
Bool: Bool,
Arr: Arr,
Obj: Obj,
Func: Func,
Err: Err,
Re: Re,
Dat: Dat,
Type: Type,
irriducible: irriducible,
struct: struct,
enums: enums,
union: union,
maybe: maybe,
tuple: tuple,
subtype: subtype,
list: list,
dict: dict,
func: func
};
}));