-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathenum.js
More file actions
203 lines (156 loc) · 3.79 KB
/
enum.js
File metadata and controls
203 lines (156 loc) · 3.79 KB
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
import Core from './core';
let Enum = {
all__qmark__: function(collection, fun = (x) => x){
for(let elem of collection){
if(!fun(elem)){
return false;
}
}
return true;
},
any__qmark__: function(collection, fun = (x) => x){
for(let elem of collection){
if(fun(elem)){
return true;
}
}
return false;
},
at: function(collection, n, the_default = null){
if(n > this.count(collection) || n < 0){
return the_default;
}
return collection[n];
},
concat: function(...enumables){
return enumables[0].concat(enumables[1]);
},
count: function(collection, fun = null){
if(fun == null){
return collection.length;
} else {
return collection.filter(fun).length;
}
},
drop: function(collection, count){
return collection.slice(count);
},
drop_while: function(collection, fun){
let count = 0;
for(let elem of collection){
if(fun(elem)){
count = count + 1;
}else{
break;
}
}
return collection.slice(count);
},
each: function(collection, fun){
for(let elem of collection){
fun(elem);
}
},
empty__qmark__: function(collection){
return collection.length === 0;
},
fetch: function(collection, n){
if(Array.isArray(collection)){
if(n < this.count(collection) && n >= 0){
return new Core.Tuple(Symbol.for("ok"), collection[n]);
}else{
return Symbol.for("error");
}
}
throw new Error("collection is not an Enumerable");
},
fetch__emark__: function(collection, n){
if(Array.isArray(collection)){
if(n < this.count(collection) && n >= 0){
return collection[n];
}else{
throw new Error("out of bounds error");
}
}
throw new Error("collection is not an Enumerable");
},
filter: function(collection, fun){
let result = [];
for(let elem of collection){
if(fun(elem)){
result.push(elem);
}
}
return result;
},
filter_map: function(collection, filter, mapper){
return Enum.map(Enum.filter(collection, filter), mapper);
},
find: function(collection, if_none = null, fun){
for(let elem of collection){
if(fun(elem)){
return elem;
}
}
return if_none;
},
into: function(collection, list){
return list.concat(collection);
},
map: function(collection, fun){
let result = [];
for(let elem of collection){
result.push(fun(elem));
}
return result;
},
map_reduce: function(collection, acc, fun){
let mapped = Object.freeze([]);
let the_acc = acc;
for (var i = 0; i < this.count(collection); i++) {
let tuple = fun(collection[i], the_acc);
the_acc = tuple.get(1);
mapped = Object.freeze(mapped.concat([tuple.get(0)]));
}
return new Core.Tuple(mapped, the_acc);
},
member__qmark__: function(collection, value){
return collection.includes(value);
},
reduce: function(collection, acc, fun){
let the_acc = acc;
for (var i = 0; i < this.count(collection); i++) {
let tuple = fun(collection[i], the_acc);
the_acc = tuple.get(1);
}
return the_acc;
},
take: function(collection, count){
return collection.slice(0, count);
},
take_every: function(collection, nth){
let result = [];
let index = 0;
for(let elem of collection){
if(index % nth === 0){
result.push(elem);
}
}
return Object.freeze(result);
},
take_while: function(collection, fun){
let count = 0;
for(let elem of collection){
if(fun(elem)){
count = count + 1;
}else{
break;
}
}
return collection.slice(0, count);
},
to_list: function(collection){
return collection;
}
};
export default Enum;