This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
forked from gkz/prelude-ls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.ls
341 lines (277 loc) · 5.81 KB
/
List.ls
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
each = (f, xs) -->
for x in xs
f x
xs
map = (f, xs) -->
[f x for x in xs]
compact = (xs) -->
[x for x in xs when x]
filter = (f, xs) -->
[x for x in xs when f x]
reject = (f, xs) -->
[x for x in xs when not f x]
remove = (el, xs) -->
i = elem-index el, xs
xs.slice!
..splice i, 1 if i?
partition = (f, xs) -->
passed = []
failed = []
for x in xs
(if f x then passed else failed).push x
[passed, failed]
find = (f, xs) -->
for x in xs when f x
return x
void
head = first = (xs) ->
xs.0
tail = (xs) ->
return void unless xs.length
xs.slice 1
last = (xs) ->
xs[*-1]
initial = (xs) ->
return void unless xs.length
xs.slice 0, -1
empty = (xs) ->
not xs.length
reverse = (xs) ->
xs.concat!.reverse!
unique = (xs) ->
result = []
for x in xs when x not in result
result.push x
result
unique-by = (f, xs) -->
seen = []
for x in xs
val = f x
continue if val in seen
seen.push val
x
fold = foldl = (f, memo, xs) -->
for x in xs
memo = f memo, x
memo
fold1 = foldl1 = (f, xs) -->
fold f, xs.0, xs.slice 1
foldr = (f, memo, xs) -->
for x in xs by -1
memo = f x, memo
memo
foldr1 = (f, xs) -->
foldr f, xs[*-1], xs.slice 0, -1
unfoldr = (f, b) -->
result = []
x = b
while (f x)?
result.push that.0
x = that.1
result
concat = (xss) ->
[].concat.apply [], xss
concat-map = (f, xs) -->
[].concat.apply [], [f x for x in xs]
flatten = (xs) -->
[].concat.apply [], [(if typeof! x is 'Array' then flatten x else x) for x in xs]
difference = (xs, ...yss) ->
results = []
:outer for x in xs
for ys in yss
continue outer if x in ys
results.push x
results
intersection = (xs, ...yss) ->
results = []
:outer for x in xs
for ys in yss
continue outer unless x in ys
results.push x
results
union = (...xss) ->
results = []
for xs in xss
for x in xs
results.push x unless x in results
results
count-by = (f, xs) -->
results = {}
for x in xs
key = f x
if key of results
results[key] += 1
else
results[key] = 1
results
group-by = (f, xs) -->
results = {}
for x in xs
key = f x
if key of results
results[key].push x
else
results[key] = [x]
results
and-list = (xs) ->
for x in xs when not x
return false
true
or-list = (xs) ->
for x in xs when x
return true
false
any = (f, xs) -->
for x in xs when f x
return true
false
all = (f, xs) -->
for x in xs when not f x
return false
true
sort = (xs) ->
xs.concat!.sort (x, y) ->
if x > y
1
else if x < y
-1
else
0
sort-with = (f, xs) -->
xs.concat!.sort f
sort-by = (f, xs) -->
xs.concat!.sort (x, y) ->
if (f x) > (f y)
1
else if (f x) < (f y)
-1
else
0
sum = (xs) ->
result = 0
for x in xs
result += x
result
product = (xs) ->
result = 1
for x in xs
result *= x
result
mean = average = (xs) ->
sum = 0
for x in xs
sum += x
sum / xs.length
maximum = (xs) ->
max = xs.0
for x in xs.slice 1 when x > max
max = x
max
minimum = (xs) ->
min = xs.0
for x in xs.slice 1 when x < min
min = x
min
maximum-by = (f, xs) -->
max = xs.0
for x in xs.slice 1 when (f x) > (f max)
max = x
max
minimum-by = (f, xs) -->
min = xs.0
for x in xs.slice 1 when (f x) < (f min)
min = x
min
scan = scanl = (f, memo, xs) -->
last = memo
[memo] ++ [last = f last, x for x in xs]
scan1 = scanl1 = (f, xs) -->
return void unless xs.length
scan f, xs.0, xs.slice 1
scanr = (f, memo, xs) -->
xs = xs.concat!.reverse!
(scan f, memo, xs).reverse!
scanr1 = (f, xs) -->
return void unless xs.length
xs = xs.concat!.reverse!
(scan f, xs.0, xs.slice 1).reverse!
slice = (x, y, xs) -->
xs.slice x, y
take = (n, xs) -->
if n <= 0
xs.slice 0, 0
else
xs.slice 0, n
drop = (n, xs) -->
if n <= 0
xs
else
xs.slice n
split-at = (n, xs) --> [(take n, xs), (drop n, xs)]
take-while = (p, xs) -->
len = xs.length
return xs unless len
i = 0
while i < len and p xs[i]
i += 1
xs.slice 0 i
drop-while = (p, xs) -->
len = xs.length
return xs unless len
i = 0
while i < len and p xs[i]
i += 1
xs.slice i
span = (p, xs) --> [(take-while p, xs), (drop-while p, xs)]
break-list = (p, xs) --> span (not) << p, xs
zip = (xs, ys) -->
result = []
len = ys.length
for x, i in xs
break if i is len
result.push [x, ys[i]]
result
zip-with = (f, xs, ys) -->
result = []
len = ys.length
for x, i in xs
break if i is len
result.push f x, ys[i]
result
zip-all = (...xss) ->
min-length = undefined
for xs in xss
min-length <?= xs.length
[[xs[i] for xs in xss] for i til min-length]
zip-all-with = (f, ...xss) ->
min-length = undefined
for xs in xss
min-length <?= xs.length
[f.apply(null, [xs[i] for xs in xss]) for i til min-length]
at = (n, xs) -->
if n < 0 then xs[xs.length + n] else xs[n]
elem-index = (el, xs) -->
for x, i in xs when x is el
return i
void
elem-indices = (el, xs) -->
[i for x, i in xs when x is el]
find-index = (f, xs) -->
for x, i in xs when f x
return i
void
find-indices = (f, xs) -->
[i for x, i in xs when f x]
module.exports = {
each, map, filter, compact, reject, remove, partition, find,
head, first, tail, last, initial, empty,
reverse, difference, intersection, union, count-by, group-by,
fold, fold1, foldl, foldl1, foldr, foldr1, unfoldr, and-list, or-list,
any, all, unique, unique-by, sort, sort-with, sort-by, sum, product, mean, average,
concat, concat-map, flatten,
maximum, minimum, maximum-by, minimum-by,
scan, scan1, scanl, scanl1, scanr, scanr1,
slice, take, drop, split-at, take-while, drop-while, span, break-list,
zip, zip-with, zip-all, zip-all-with,
at, elem-index, elem-indices, find-index, find-indices,
}