forked from elixirscript/elixirscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum.js
More file actions
222 lines (169 loc) · 4.51 KB
/
Copy pathenum.js
File metadata and controls
222 lines (169 loc) · 4.51 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import Kernel from './kernel';
import JS from './js';
let Enum = {
all__qmark__: function* (collection, fun = (x) => x){
for(let elem of collection){
let the_result = yield* JS.run(fun, [elem]);
if(!the_result){
return yield false;
}
}
return yield true;
},
any__qmark__: function* (collection, fun = (x) => x){
for(let elem of collection){
let the_result = yield* JS.run(fun, [elem]);
if(!the_result){
return yield true;
}
}
return yield 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){
yield collection.length;
} else {
let result = 0;
for(let elem of collection){
let the_result = yield* JS.run(fun, [elem]);
if(the_result){
result++;
}
}
return yield result;
}
},
drop: function(collection, count){
return collection.slice(count);
},
drop_while: function* (collection, fun){
let count = 0;
for(let elem of collection){
let the_result = yield* JS.run(fun, [elem]);
if(the_result){
count = count + 1;
}else{
break;
}
}
return yield collection.slice(count);
},
each: function* (collection, fun){
for(let elem of collection){
yield* JS.run(fun, [elem]);
}
},
empty__qmark__: function(collection){
return collection.length === 0;
},
fetch: function(collection, n){
if(Kernel.is_list(collection)){
if(n < this.count(collection) && n >= 0){
return Kernel.SpecialForms.tuple(Kernel.SpecialForms.atom("ok"), collection[n]);
}else{
return Kernel.SpecialForms.atom("error");
}
}
throw new Error("collection is not an Enumerable");
},
fetch__emark__: function(collection, n){
if(Kernel.is_list(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){
let the_result = yield* JS.run(fun, [elem]);
if(the_result){
result.push(elem);
}
}
return yield 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(yield* JS.run(fun, [elem])){
return yield elem;
}
}
return yield if_none;
},
into: function* (collection, list){
return yield list.concat(collection);
},
map: function*(collection, fun){
let result = [];
for(let elem of collection){
let the_result = yield* JS.run(fun, [elem]);
result.push(the_result);
}
return yield result;
},
map_reduce: function* (collection, acc, fun){
let mapped = Kernel.SpecialForms.list();
let the_acc = acc;
for (var i = 0; i < this.count(collection); i++) {
let tuple = yield* JS.run(fun, [collection[i], the_acc]);
the_acc = Kernel.elem(tuple, 1);
mapped = Kernel.SpecialForms.list(...mapped.concat([Kernel.elem(tuple, 0)]));
}
return yield Kernel.SpecialForms.tuple(mapped, the_acc);
},
member: 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 = yield* JS.run(fun, [collection[i], the_acc]);
the_acc = Kernel.elem(tuple, 1);
}
return yield 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 Kernel.SpecialForms.list(...result);
},
take_while: function* (collection, fun){
let count = 0;
for(let elem of collection){
if(yield* JS.run(fun, [elem])){
count = count + 1;
}else{
break;
}
}
return yield collection.slice(0, count);
},
to_list: function(collection){
return collection;
}
};
export default Enum;