forked from elm/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.js
305 lines (268 loc) · 7.08 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
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
Elm.Native.List = {};
Elm.Native.List.make = function(elm) {
elm.Native = elm.Native || {};
elm.Native.List = elm.Native.List || {};
if (elm.Native.List.values) return elm.Native.List.values;
if ('values' in Elm.Native.List)
return elm.Native.List.values = Elm.Native.List.values;
var Utils = Elm.Native.Utils.make(elm);
var Nil = Utils.Nil;
var Cons = Utils.Cons;
function throwError(f) {
throw new Error("Function '" + f + "' expects a non-empty list!");
}
function toArray(xs) {
var out = [];
while (xs.ctor !== '[]') {
out.push(xs._0);
xs = xs._1;
}
return out;
}
function fromArray(arr) {
var out = Nil;
for (var i = arr.length; i--; ) {
out = Cons(arr[i], out);
}
return out;
}
function range(lo,hi) {
var lst = Nil;
if (lo <= hi) {
do { lst = Cons(hi,lst) } while (hi-->lo);
}
return lst
}
function head(v) {
return v.ctor === '[]' ? throwError('head') : v._0;
}
function tail(v) {
return v.ctor === '[]' ? throwError('tail') : v._1;
}
function map(f, xs) {
var arr = [];
while (xs.ctor !== '[]') {
arr.push(f(xs._0));
xs = xs._1;
}
return fromArray(arr);
}
// f defined similarly for both foldl and foldr (NB: different from Haskell)
// ie, foldl : (a -> b -> b) -> b -> [a] -> b
function foldl(f, b, xs) {
var acc = b;
while (xs.ctor !== '[]') {
acc = A2(f, xs._0, acc);
xs = xs._1;
}
return acc;
}
function foldr(f, b, xs) {
var arr = toArray(xs);
var acc = b;
for (var i = arr.length; i--; ) {
acc = A2(f, arr[i], acc);
}
return acc;
}
function foldl1(f, xs) {
return xs.ctor === '[]' ? throwError('foldl1') : foldl(f, xs._0, xs._1);
}
function foldr1(f, xs) {
if (xs.ctor === '[]') { throwError('foldr1'); }
var arr = toArray(xs);
var acc = arr.pop();
for (var i = arr.length; i--; ) {
acc = A2(f, arr[i], acc);
}
return acc;
}
function scanl(f, b, xs) {
var arr = toArray(xs);
arr.unshift(b);
var len = arr.length;
for (var i = 1; i < len; ++i) {
arr[i] = A2(f, arr[i], arr[i-1]);
}
return fromArray(arr);
}
function scanl1(f, xs) {
return xs.ctor === '[]' ? throwError('scanl1') : scanl(f, xs._0, xs._1);
}
function filter(pred, xs) {
var arr = [];
while (xs.ctor !== '[]') {
if (pred(xs._0)) { arr.push(xs._0); }
xs = xs._1;
}
return fromArray(arr);
}
function length(xs) {
var out = 0;
while (xs.ctor !== '[]') {
out += 1;
xs = xs._1;
}
return out;
}
function member(x, xs) {
while (xs.ctor !== '[]') {
if (Utils.eq(x,xs._0)) return true;
xs = xs._1;
}
return false;
}
function append(xs, ys) {
if (xs.ctor === '[]') {
return ys;
}
var root = Cons(xs._0, Nil);
var curr = root;
xs = xs._1;
while (xs.ctor !== '[]') {
curr._1 = Cons(xs._0, Nil);
xs = xs._1;
curr = curr._1;
}
curr._1 = ys;
return root;
}
function all(pred, xs) {
while (xs.ctor !== '[]') {
if (!pred(xs._0)) return false;
xs = xs._1;
}
return true;
}
function any(pred, xs) {
while (xs.ctor !== '[]') {
if (pred(xs._0)) return true;
xs = xs._1;
}
return false;
}
function map2(f, xs, ys) {
var arr = [];
while (xs.ctor !== '[]' && ys.ctor !== '[]') {
arr.push(A2(f, xs._0, ys._0));
xs = xs._1;
ys = ys._1;
}
return fromArray(arr);
}
function map3(f, xs, ys, zs) {
var arr = [];
while (xs.ctor !== '[]' && ys.ctor !== '[]' && zs.ctor !== '[]') {
arr.push(A3(f, xs._0, ys._0, zs._0));
xs = xs._1;
ys = ys._1;
zs = zs._1;
}
return fromArray(arr);
}
function map4(f, ws, xs, ys, zs) {
var arr = [];
while ( ws.ctor !== '[]'
&& xs.ctor !== '[]'
&& ys.ctor !== '[]'
&& zs.ctor !== '[]')
{
arr.push(A4(f, ws._0, xs._0, ys._0, zs._0));
ws = ws._1;
xs = xs._1;
ys = ys._1;
zs = zs._1;
}
return fromArray(arr);
}
function map5(f, vs, ws, xs, ys, zs) {
var arr = [];
while ( vs.ctor !== '[]'
&& ws.ctor !== '[]'
&& xs.ctor !== '[]'
&& ys.ctor !== '[]'
&& zs.ctor !== '[]')
{
arr.push(A5(f, vs._0, ws._0, xs._0, ys._0, zs._0));
vs = vs._1;
ws = ws._1;
xs = xs._1;
ys = ys._1;
zs = zs._1;
}
return fromArray(arr);
}
function sort(xs) {
return fromArray(toArray(xs).sort(Utils.cmp));
}
function sortBy(f, xs) {
return fromArray(toArray(xs).sort(function(a,b){
return Utils.cmp(f(a), f(b));
}));
}
function sortWith(f, xs) {
return fromArray(toArray(xs).sort(function(a,b){
var ord = f(a)(b).ctor;
return ord === 'EQ' ? 0 : ord === 'LT' ? -1 : 1;
}));
}
function take(n, xs) {
var arr = [];
while (xs.ctor !== '[]' && n > 0) {
arr.push(xs._0);
xs = xs._1;
--n;
}
return fromArray(arr);
}
function drop(n, xs) {
while (xs.ctor !== '[]' && n > 0) {
xs = xs._1;
--n;
}
return xs;
}
function repeat(n, x) {
var arr = [];
var pattern = [x];
while (n > 0) {
if (n & 1) arr = arr.concat(pattern);
n >>= 1, pattern = pattern.concat(pattern);
}
return fromArray(arr);
}
Elm.Native.List.values = {
Nil:Nil,
Cons:Cons,
cons:F2(Cons),
toArray:toArray,
fromArray:fromArray,
range:range,
append: F2(append),
head:head,
tail:tail,
map:F2(map),
foldl:F3(foldl),
foldr:F3(foldr),
foldl1:F2(foldl1),
foldr1:F2(foldr1),
scanl:F3(scanl),
scanl1:F2(scanl1),
filter:F2(filter),
length:length,
member:F2(member),
all:F2(all),
any:F2(any),
map2:F3(map2),
map3:F4(map3),
map4:F5(map4),
map5:F6(map5),
sort:sort,
sortBy:F2(sortBy),
sortWith:F2(sortWith),
take:F2(take),
drop:F2(drop),
repeat:F2(repeat)
};
return elm.Native.List.values = Elm.Native.List.values;
};