Source file TableclothList.ml
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
type 'a t = 'a list
let empty = []
let singleton = Base.List.return
let repeat element ~times =
if times < 0 then [] else Base.List.init times ~f:(fun _ -> element)
let rec range ?(from = 0) to_ =
if from >= to_ then [] else from :: range ~from:(from + 1) to_
let initialize = Base.List.init
let sum (type a) (a : a t) (module M : TableclothContainer.Sum with type t = a)
: a =
Base.List.fold a ~init:M.zero ~f:M.add
let fromArray = Base.Array.to_list
let from_array = fromArray
let isEmpty (l : 'a list) : bool = l = []
let is_empty = isEmpty
let head = Base.List.hd
let tail = Base.List.tl
let cons list element = element :: list
let take t ~count = Base.List.take t count
let takeWhile (l : 'a list) ~(f : 'a -> bool) : 'a list =
let rec takeWhileHelper acc l' =
match l' with
| [] ->
Base.List.rev acc
| x :: rest ->
if f x then takeWhileHelper (x :: acc) rest else Base.List.rev acc
in
takeWhileHelper [] l
let take_while = takeWhile
let drop t ~count = Base.List.drop t count
let rec dropWhile (l : 'a list) ~(f : 'a -> bool) : 'a list =
match l with [] -> [] | x :: rest -> if f x then dropWhile ~f rest else l
let drop_while = dropWhile
let initial (l : 'a list) : 'a list option =
match Base.List.rev l with
| [] ->
None
| _ :: rest ->
Some (Base.List.rev rest)
let rec last (l : 'a list) : 'a option =
match l with [] -> None | [ a ] -> Some a | _ :: tail -> last tail
let append (l1 : 'a list) (l2 : 'a list) : 'a list = Base.List.append l1 l2
let flatten = Base.List.concat
let reverse (l : 'a list) : 'a list = Base.List.rev l
let map2 listA listB ~f =
let mapped, _ =
Base.List.fold listA ~init:([], listB) ~f:(fun (result, lb) a ->
match lb with b :: rest -> (f a b :: result, rest) | [] -> (result, []) )
in
reverse mapped
let map3 listA listB listC ~f =
let mapped, _, _ =
Base.List.fold listA ~init:([], listB, listC) ~f:(fun (result, lb, lc) a ->
match lb with
| b :: rest1 ->
( match lc with
| c :: rest2 ->
(f a b c :: result, rest1, rest2)
| [] ->
(result, [], []) )
| [] ->
(result, [], []) )
in
reverse mapped
let map = Base.List.map
let mapWithIndex = Base.List.mapi
let map_with_index = mapWithIndex
let flatMap = Base.List.concat_map
let flat_map = flatMap
let includes = Base.List.mem
let uniqueBy ~(f : 'a -> string) (l : 'a list) : 'a list =
let rec uniqueHelper
~(f : 'a -> string)
(existing : Base.Set.M(Base.String).t)
(remaining : 'a list)
(accumulator : 'a list) : 'a list =
match remaining with
| [] ->
reverse accumulator
| first :: rest ->
let computedFirst = f first in
if Base.Set.mem existing computedFirst
then uniqueHelper ~f existing rest accumulator
else
uniqueHelper
~f
(Base.Set.add existing computedFirst)
rest
(first :: accumulator)
in
uniqueHelper ~f (Base.Set.empty (module Base.String)) l []
let unique_by = uniqueBy
let find = Base.List.find
let findIndex = Base.List.findi
let find_index = findIndex
let any = Base.List.exists
let all = Base.List.for_all
let getAt (l : 'a list) ~(index : int) : 'a option = Base.List.nth l index
let get_at = getAt
let filterMap = Base.List.filter_map
let filter_map = filterMap
let filter t ~f = Base.List.filter t ~f
let filterWithIndex t ~f = Base.List.filteri t ~f
let filter_with_index = filterWithIndex
let partition = Base.List.partition_tf
let fold t ~initial ~f = Base.List.fold t ~init:initial ~f
let count = Base.List.count
let foldRight t ~initial ~f =
Base.List.fold_right t ~init:initial ~f:(Fun.flip f)
let fold_right = foldRight
let splitAt (l : 'a list) ~(index : int) : 'a list * 'a list =
(take ~count:index l, drop ~count:index l)
let split_at = splitAt
let splitWhen (l : 'a list) ~(f : 'a -> bool) : 'a list * 'a list =
match findIndex ~f:(fun _ element -> f element) l with
| Some (index, _) ->
splitAt ~index l
| None ->
(l, [])
let split_when = splitWhen
let updateAt (l : 'a list) ~(index : int) ~(f : 'a -> 'a) : 'a list =
if index < 0
then l
else
let front, back = splitAt ~index l in
match back with [] -> l | x :: rest -> append front (f x :: rest)
let update_at = updateAt
let length (l : 'a list) : int = List.length l
let removeAt (l : 'a list) ~(index : int) : 'a list =
if index < 0
then l
else
let front, back = splitAt ~index l in
match tail back with None -> l | Some t -> append front t
let remove_at = removeAt
let minimumBy ~(f : 'a -> 'comparable) (ls : 'a list) : 'a option =
let minBy (y, fy) x =
let fx = f x in
if fx < fy then (x, fx) else (y, fy)
in
match ls with
| [ l ] ->
Some l
| l1 :: lrest ->
Some (fst (fold ~f:minBy ~initial:(l1, f l1) lrest))
| _ ->
None
let minimum_by = minimumBy
let maximumBy ~(f : 'a -> 'comparable) (l : 'a list) : 'a option =
let maxBy (y, fy) x =
let fx = f x in
if fx > fy then (x, fx) else (y, fy)
in
match l with
| [] ->
None
| [ x ] ->
Some x
| x :: rest ->
Some (fst (fold ~f:maxBy ~initial:(x, f x) rest))
let maximum_by = maximumBy
let minimum = Base.List.min_elt
let maximum = Base.List.max_elt
let extent t ~compare =
fold t ~initial:None ~f:(fun current element ->
match current with
| None ->
Some (element, element)
| Some (min, max) ->
Some
( ( match compare element min < 0 with
| true ->
element
| false ->
min )
, match compare element max > 0 with
| true ->
element
| false ->
max ) )
let insertAt (t : 'a list) ~(index : int) ~(value : 'a) : 'a list =
let front, back = splitAt t ~index in
append front (value :: back)
let insert_at = insertAt
let zip listA listB = map2 listA listB ~f:(fun x y -> (x, y))
let unzip = Base.List.unzip
let sliding ?(step = 1) (t : 'a t) ~(size : int) : 'a t t =
let rec takeAllOrEmpty t n (current, count) =
if count = n
then reverse current
else
match t with
| [] ->
[]
| x :: xs ->
takeAllOrEmpty xs n (x :: current, count + 1)
in
let rec loop t =
if isEmpty t
then []
else
let sample = takeAllOrEmpty t size ([], 0) in
if isEmpty sample then [] else sample :: loop (Base.List.drop t step)
in
loop t
let chunksOf t ~size = sliding t ~step:size ~size
let chunks_of = chunksOf
let intersperse (l : 'a list) ~sep : 'a list =
match l with
| [] ->
[]
| [ x ] ->
[ x ]
| x :: rest ->
x :: foldRight rest ~initial:[] ~f:(fun acc x -> sep :: x :: acc)
let forEach l ~f = Base.List.iter l ~f
let for_each = forEach
let forEachWithIndex = Base.List.iteri
let for_each_with_index = forEachWithIndex
let toArray = Base.List.to_array
let to_array = toArray
let groupWhile t ~f = Base.List.group t ~break:f
let group_while = groupWhile
let sort = Base.List.sort
let sortBy ~(f : 'a -> 'b) (l : 'a list) : 'a list =
Base.List.sort l ~compare:(fun a b ->
let a' = f a in
let b' = f b in
if a' = b' then 0 else if a' < b' then -1 else 1 )
let sort_by = sortBy
let join t ~sep = Stdlib.String.concat sep t
let groupBy t comparator ~f =
fold t ~initial:(TableclothMap.empty comparator) ~f:(fun map element ->
let key = f element in
TableclothMap.update map ~key ~f:(function
| None ->
Some [ element ]
| Some elements ->
Some (element :: elements) ) )
let group_by = groupBy
let rec equal equalElement a b =
match (a, b) with
| [], [] ->
true
| x :: xs, y :: ys ->
equalElement x y && equal equalElement xs ys
| _ ->
false
let rec compare compareElement a b =
match (a, b) with
| [], [] ->
0
| [], _ ->
-1
| _, [] ->
1
| x :: xs, y :: ys ->
( match compareElement x y with
| 0 ->
compare compareElement xs ys
| result ->
result )