forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext_list.ml
419 lines (345 loc) · 10.2 KB
/
ext_list.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
let rec filter_map (f: 'a -> 'b option) xs =
match xs with
| [] -> []
| y :: ys ->
begin match f y with
| None -> filter_map f ys
| Some z -> z :: filter_map f ys
end
let excludes (p : 'a -> bool ) l : bool * 'a list=
let excluded = ref false in
let rec aux accu = function
| [] -> List.rev accu
| x :: l ->
if p x then
begin
excluded := true ;
aux accu l
end
else aux (x :: accu) l in
let v = aux [] l in
if !excluded then true, v else false,l
let exclude_with_fact p l =
let excluded = ref None in
let rec aux accu = function
| [] -> List.rev accu
| x :: l ->
if p x then
begin
excluded := Some x ;
aux accu l
end
else aux (x :: accu) l in
let v = aux [] l in
!excluded , if !excluded <> None then v else l
(** Make sure [p2 x] and [p1 x] will not hold at the same time *)
let exclude_with_fact2 p1 p2 l =
let excluded1 = ref None in
let excluded2 = ref None in
let rec aux accu = function
| [] -> List.rev accu
| x :: l ->
if p1 x then
begin
excluded1 := Some x ;
aux accu l
end
else if p2 x then
begin
excluded2 := Some x ;
aux accu l
end
else aux (x :: accu) l in
let v = aux [] l in
!excluded1, !excluded2 , if !excluded1 <> None && !excluded2 <> None then v else l
let rec same_length xs ys =
match xs, ys with
| [], [] -> true
| _::xs, _::ys -> same_length xs ys
| _, _ -> false
let filter_mapi (f: int -> 'a -> 'b option) xs =
let rec aux i xs =
match xs with
| [] -> []
| y :: ys ->
begin match f i y with
| None -> aux (i + 1) ys
| Some z -> z :: aux (i + 1) ys
end in
aux 0 xs
let rec filter_map2 (f: 'a -> 'b -> 'c option) xs ys =
match xs,ys with
| [],[] -> []
| u::us, v :: vs ->
begin match f u v with
| None -> filter_map2 f us vs (* idea: rec f us vs instead? *)
| Some z -> z :: filter_map2 f us vs
end
| _ -> invalid_arg "Ext_list.filter_map2"
let filter_map2i (f: int -> 'a -> 'b -> 'c option) xs ys =
let rec aux i xs ys =
match xs,ys with
| [],[] -> []
| u::us, v :: vs ->
begin match f i u v with
| None -> aux (i + 1) us vs (* idea: rec f us vs instead? *)
| Some z -> z :: aux (i + 1) us vs
end
| _ -> invalid_arg "Ext_list.filter_map2i" in
aux 0 xs ys
let rec rev_map_append f l1 l2 =
match l1 with
| [] -> l2
| a :: l -> rev_map_append f l (f a :: l2)
let flat_map2 f lx ly =
let rec aux acc lx ly =
match lx, ly with
| [], []
-> List.rev acc
| x::xs, y::ys
-> aux (List.rev_append (f x y) acc) xs ys
| _, _ -> invalid_arg "Ext_list.flat_map2" in
aux [] lx ly
let rec flat_map_aux f acc append lx =
match lx with
| [] -> List.rev_append acc append
| y::ys -> flat_map_aux f (List.rev_append ( f y) acc ) append ys
let flat_map f lx =
flat_map_aux f [] [] lx
let flat_map_acc f append lx = flat_map_aux f [] append lx
let rec map2_last f l1 l2 =
match (l1, l2) with
| ([], []) -> []
| [u], [v] -> [f true u v ]
| (a1::l1, a2::l2) -> let r = f false a1 a2 in r :: map2_last f l1 l2
| (_, _) -> invalid_arg "List.map2_last"
let rec map_last f l1 =
match l1 with
| [] -> []
| [u]-> [f true u ]
| a1::l1 -> let r = f false a1 in r :: map_last f l1
let rec fold_right2_last f l1 l2 accu =
match (l1, l2) with
| ([], []) -> accu
| [last1], [last2] -> f true last1 last2 accu
| (a1::l1, a2::l2) -> f false a1 a2 (fold_right2_last f l1 l2 accu)
| (_, _) -> invalid_arg "List.fold_right2"
let init n f =
Array.to_list (Array.init n f)
let take n l =
let arr = Array.of_list l in
let arr_length = Array.length arr in
if arr_length < n then invalid_arg "Ext_list.take"
else (Array.to_list (Array.sub arr 0 n ),
Array.to_list (Array.sub arr n (arr_length - n)))
let try_take n l =
let arr = Array.of_list l in
let arr_length = Array.length arr in
if arr_length <= n then
l, arr_length, []
else Array.to_list (Array.sub arr 0 n ), n, (Array.to_list (Array.sub arr n (arr_length - n)))
let rec length_compare l n =
if n < 0 then `Gt
else
begin match l with
| _ ::xs -> length_compare xs (n - 1)
| [] ->
if n = 0 then `Eq
else `Lt
end
(**
{[length xs = length ys + n ]}
*)
let rec length_larger_than_n n xs ys =
match xs, ys with
| _, [] -> length_compare xs n = `Eq
| _::xs, _::ys ->
length_larger_than_n n xs ys
| [], _ -> false
let exclude_tail (x : 'a list) =
let rec aux acc x =
match x with
| [] -> invalid_arg "Ext_list.exclude_tail"
| [ x ] -> x, List.rev acc
| y0::ys -> aux (y0::acc) ys in
aux [] x
(* For small list, only need partial equality
{[
group (=) [1;2;3;4;3]
;;
- : int list list = [[3; 3]; [4]; [2]; [1]]
# group (=) [];;
- : 'a list list = []
]}
*)
let rec group (cmp : 'a -> 'a -> bool) (lst : 'a list) : 'a list list =
match lst with
| [] -> []
| x::xs ->
aux cmp x (group cmp xs )
and aux cmp (x : 'a) (xss : 'a list list) : 'a list list =
match xss with
| [] -> [[x]]
| y::ys ->
if cmp x (List.hd y) (* cannot be null*) then
(x::y) :: ys
else
y :: aux cmp x ys
let stable_group cmp lst = group cmp lst |> List.rev
let rec drop n h =
if n < 0 then invalid_arg "Ext_list.drop"
else if n = 0 then h
else if h = [] then invalid_arg "Ext_list.drop"
else
drop (n - 1) (List.tl h)
let rec for_all_ret p = function
| [] -> None
| a::l ->
if p a
then for_all_ret p l
else Some a
let rec for_all_opt p = function
| [] -> None
| a::l ->
match p a with
| None -> for_all_opt p l
| v -> v
let fold f l init =
List.fold_left (fun acc i -> f i init) init l
let rev_map_acc acc f l =
let rec rmap_f accu = function
| [] -> accu
| a::l -> rmap_f (f a :: accu) l
in
rmap_f acc l
let rec map_acc acc f l =
match l with
| [] -> acc
| h::hs -> f h :: map_acc acc f hs
let rec rev_iter f xs =
match xs with
| [] -> ()
| y :: ys ->
rev_iter f ys ;
f y
let rec for_all2_no_exn p l1 l2 =
match (l1, l2) with
| ([], []) -> true
| (a1::l1, a2::l2) -> p a1 a2 && for_all2_no_exn p l1 l2
| (_, _) -> false
let rec find_no_exn p = function
| [] -> None
| x :: l -> if p x then Some x else find_no_exn p l
let rec find_opt p = function
| [] -> None
| x :: l ->
match p x with
| Some _ as v -> v
| None -> find_opt p l
let split_map
( f : 'a -> ('b * 'c)) (xs : 'a list ) : 'b list * 'c list =
let rec aux bs cs xs =
match xs with
| [] -> List.rev bs, List.rev cs
| u::us ->
let b,c = f u in aux (b::bs) (c ::cs) us in
aux [] [] xs
(*
{[
reduce_from_right (-) [1;2;3];;
- : int = 2
# reduce_from_right (-) [1;2;3; 4];;
- : int = -2
# reduce_from_right (-) [1];;
- : int = 1
# reduce_from_right (-) [1;2;3; 4; 5];;
- : int = 3
]}
*)
let reduce_from_right fn lst =
begin match List.rev lst with
| last :: rest ->
List.fold_left (fun x y -> fn y x) last rest
| _ -> invalid_arg "Ext_list.reduce"
end
let reduce_from_left fn lst =
match lst with
| first :: rest -> List.fold_left fn first rest
| _ -> invalid_arg "Ext_list.reduce_from_left"
type 'a t = 'a list ref
let create_ref_empty () = ref []
let ref_top x =
match !x with
| y::_ -> y
| _ -> invalid_arg "Ext_list.ref_top"
let ref_empty x =
match !x with [] -> true | _ -> false
let ref_push x refs =
refs := x :: !refs
let ref_pop refs =
match !refs with
| [] -> invalid_arg "Ext_list.ref_pop"
| x::rest ->
refs := rest ;
x
let rev_except_last xs =
let rec aux acc xs =
match xs with
| [ ] -> invalid_arg "Ext_list.rev_except_last"
| [ x ] -> acc ,x
| x :: xs -> aux (x::acc) xs in
aux [] xs
let sort_via_array cmp lst =
let arr = Array.of_list lst in
Array.sort cmp arr;
Array.to_list arr
let rec last xs =
match xs with
| [x] -> x
| _ :: tl -> last tl
| [] -> invalid_arg "Ext_list.last"
let rec assoc_by_string def (k : string) lst =
match lst with
| [] ->
begin match def with
| None -> assert false
| Some x -> x end
| (k1,v1)::rest ->
if Ext_string.equal k1 k then v1 else
assoc_by_string def k rest
let rec assoc_by_int def (k : int) lst =
match lst with
| [] ->
begin match def with
| None -> assert false
| Some x -> x end
| (k1,v1)::rest ->
if k1 = k then v1 else
assoc_by_int def k rest
(** `modulo [1;2;3;4] [1;2;3]` => [1;2;3], Some [4] `
modulo [1;2;3] [1;2;3;4] => [1;2;3] None
modulo [1;2;3] [1;2;3] => [1;2;3] Some []
*)