-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
List.js
112 lines (87 loc) · 2.9 KB
/
List.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
(function(f) {
'use strict';
if (typeof module === 'object' && typeof module.exports === 'object') {
module.exports = f (require ('sanctuary-def'),
require ('sanctuary-show'),
require ('sanctuary-type-classes'),
require ('sanctuary-type-identifiers'));
} else {
self.List = f (self.sanctuaryDef,
self.sanctuaryShow,
self.sanctuaryTypeClasses,
self.sanctuaryTypeIdentifiers);
}
} (($, show, Z, type) => {
'use strict';
const List = {prototype: _List.prototype};
List.prototype.constructor = List;
function _List(tag, head, tail) {
this.isCons = tag === 'Cons';
this.isNil = tag === 'Nil';
if (this.isCons) {
this.head = head;
this.tail = tail;
}
}
// Nil :: List a
const Nil = List.Nil = new _List ('Nil');
// Cons :: (a, List a) -> List a
const Cons = List.Cons = head => tail => new _List ('Cons', head, tail);
// listTypeIdent :: String
const listTypeIdent = List.prototype['@@type'] = 'sanctuary-site/List';
List['fantasy-land/empty'] = () => Nil;
List['fantasy-land/of'] = x => Cons (x) (Nil);
List['fantasy-land/zero'] = List['fantasy-land/empty'];
List.prototype['fantasy-land/equals'] = function(other) {
return this.isNil ?
other.isNil :
other.isCons &&
Z.equals (this.head, other.head) &&
Z.equals (this.tail, other.tail);
};
List.prototype['fantasy-land/concat'] = function(other) {
return this.isNil ?
other :
Cons (this.head) (Z.concat (this.tail, other));
};
List.prototype['fantasy-land/map'] = function(f) {
return this.isNil ?
Nil :
Cons (f (this.head)) (Z.map (f, this.tail));
};
List.prototype['fantasy-land/ap'] = function(other) {
return this.isNil || other.isNil ?
Nil :
Z.concat (Z.map (other.head, this), Z.ap (other.tail, this));
};
List.prototype['fantasy-land/chain'] = function(f) {
return this.isNil ?
Nil :
Z.concat (f (this.head), Z.chain (f, this.tail));
};
List.prototype['fantasy-land/alt'] = List.prototype['fantasy-land/concat'];
List.prototype['fantasy-land/reduce'] = function(f, x) {
return this.isNil ?
x :
Z.reduce (f, f (x, this.head), this.tail);
};
List.prototype['fantasy-land/traverse'] = function(typeRep, f) {
return this.isNil ?
Z.of (typeRep, Nil) :
Z.ap (Z.map (Cons, f (this.head)),
Z.traverse (typeRep, f, this.tail));
};
List.prototype['@@show'] = function() {
return this.isNil ?
'Nil' :
'Cons (' + show (this.head) + ') (' + show (this.tail) + ')';
};
// List.Type :: Type -> Type
List.Type = $.UnaryType
('List')
('https://github.com/sanctuary-js/sanctuary-site/blob/gh-pages/adt/List.js')
([])
(x => type (x) === listTypeIdent)
(list => list);
return List;
}));