Source file small_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
type 'a t =
| Tuple0
| Tuple1 of 'a
| Tuple2 of 'a * 'a
| Tuple3 of 'a * 'a * 'a
| Tuple4 of 'a * 'a * 'a * 'a
| Tuple5 of 'a * 'a * 'a * 'a * 'a
| Tuple6 of 'a * 'a * 'a * 'a * 'a * 'a
| Many of 'a * 'a * 'a * 'a * 'a * 'a * 'a * 'a list
let empty = Tuple0
let cons x = function
| Tuple0 -> Tuple1 x
| Tuple1 a -> Tuple2 (x, a)
| Tuple2 (a, b) -> Tuple3 (x, a, b)
| Tuple3 (a, b, c) -> Tuple4 (x, a, b, c)
| Tuple4 (a, b, c, d) -> Tuple5 (x, a, b, c, d)
| Tuple5 (a, b, c, d, e) -> Tuple6 (x, a, b, c, d, e)
| Tuple6 (a, b, c, d, e, f) -> Many (x, a, b, c, d, e, f, [])
| Many (a, b, c, d, e, f, g, l) -> Many (x, a, b, c, d, e, f, g :: l)
let map ~f:fn = function
| Tuple0 -> Tuple0
| Tuple1 a -> Tuple1 (fn a)
| Tuple2 (a, b) -> Tuple2 (fn a, fn b)
| Tuple3 (a, b, c) -> Tuple3 (fn a, fn b, fn c)
| Tuple4 (a, b, c, d) -> Tuple4 (fn a, fn b, fn c, fn d)
| Tuple5 (a, b, c, d, e) -> Tuple5 (fn a, fn b, fn c, fn d, fn e)
| Tuple6 (a, b, c, d, e, f) -> Tuple6 (fn a, fn b, fn c, fn d, fn e, fn f)
| Many (a, b, c, d, e, f, g, l) ->
Many (fn a, fn b, fn c, fn d, fn e, fn f, fn g, List.map fn l)
let iter ~f:fn = function
| Tuple0 -> ()
| Tuple1 a -> fn a
| Tuple2 (a, b) ->
fn a;
fn b
| Tuple3 (a, b, c) ->
fn a;
fn b;
fn c
| Tuple4 (a, b, c, d) ->
fn a;
fn b;
fn c;
fn d
| Tuple5 (a, b, c, d, e) ->
fn a;
fn b;
fn c;
fn d;
fn e
| Tuple6 (a, b, c, d, e, f) ->
fn a;
fn b;
fn c;
fn d;
fn e;
fn f
| Many (a, b, c, d, e, f, g, l) ->
fn a;
fn b;
fn c;
fn d;
fn e;
fn f;
fn g;
List.iter fn l
let exists ~f:fn = function
| Tuple0 -> false
| Tuple1 a -> fn a
| Tuple2 (a, b) -> fn a || fn b
| Tuple3 (a, b, c) -> fn a || fn b || fn c
| Tuple4 (a, b, c, d) -> fn a || fn b || fn c || fn d
| Tuple5 (a, b, c, d, e) -> fn a || fn b || fn c || fn d || fn e
| Tuple6 (a, b, c, d, e, f) -> fn a || fn b || fn c || fn d || fn e || fn f
| Many (a, b, c, d, e, f, g, l) ->
fn a || fn b || fn c || fn d || fn e || fn f || fn g || List.exists fn l
let rec list_find_map f = function
| [] -> None
| x :: l -> (
match f x with Some _ as result -> result | None -> list_find_map f l)
let[@ocamlformat "disable"] find_map ~f:fn = function
| Tuple0 -> raise Not_found
| Tuple1 a -> fn a
| Tuple2 (a, b) -> (
match fn a with Some _ as r -> r | None ->
fn b)
| Tuple3 (a, b, c) -> (
match fn a with Some _ as r -> r | None ->
match fn b with Some _ as r -> r | None ->
fn c)
| Tuple4 (a, b, c, d) -> (
match fn a with Some _ as r -> r | None ->
match fn b with Some _ as r -> r | None ->
match fn c with Some _ as r -> r | None ->
fn d)
| Tuple5 (a, b, c, d, e) -> (
match fn a with Some _ as r -> r | None ->
match fn b with Some _ as r -> r | None ->
match fn c with Some _ as r -> r | None ->
match fn d with Some _ as r -> r | None ->
fn e)
| Tuple6 (a, b, c, d, e, f) -> (
match fn a with Some _ as r -> r | None ->
match fn b with Some _ as r -> r | None ->
match fn c with Some _ as r -> r | None ->
match fn d with Some _ as r -> r | None ->
match fn e with Some _ as r -> r | None ->
fn f)
| Many (a, b, c, d, e, f, g, l) -> (
match fn a with Some _ as r -> r | None ->
match fn b with Some _ as r -> r | None ->
match fn c with Some _ as r -> r | None ->
match fn d with Some _ as r -> r | None ->
match fn e with Some _ as r -> r | None ->
match fn f with Some _ as r -> r | None ->
match fn g with Some _ as r -> r | None ->
list_find_map fn l)
let to_list = function
| Tuple0 -> []
| Tuple1 a -> [ a ]
| Tuple2 (a, b) -> [ a; b ]
| Tuple3 (a, b, c) -> [ a; b; c ]
| Tuple4 (a, b, c, d) -> [ a; b; c; d ]
| Tuple5 (a, b, c, d, e) -> [ a; b; c; d; e ]
| Tuple6 (a, b, c, d, e, f) -> [ a; b; c; d; e; f ]
| Many (a, b, c, d, e, f, g, l) -> a :: b :: c :: d :: e :: f :: g :: l
let to_array = function
| Tuple0 -> [||]
| Tuple1 a -> [| a |]
| Tuple2 (a, b) -> [| a; b |]
| Tuple3 (a, b, c) -> [| a; b; c |]
| Tuple4 (a, b, c, d) -> [| a; b; c; d |]
| Tuple5 (a, b, c, d, e) -> [| a; b; c; d; e |]
| Tuple6 (a, b, c, d, e, f) -> [| a; b; c; d; e; f |]
| Many (a, b, c, d, e, f, g, l) ->
let len = 7 + List.length l in
let arr = Array.make len a in
arr.(1) <- b;
arr.(2) <- c;
arr.(3) <- d;
arr.(4) <- e;
arr.(5) <- f;
arr.(6) <- g;
List.iteri (fun i elt -> arr.(i + 7) <- elt) l;
arr
let of_list = function
| [] -> Tuple0
| [ a ] -> Tuple1 a
| [ a; b ] -> Tuple2 (a, b)
| [ a; b; c ] -> Tuple3 (a, b, c)
| [ a; b; c; d ] -> Tuple4 (a, b, c, d)
| [ a; b; c; d; e ] -> Tuple5 (a, b, c, d, e)
| [ a; b; c; d; e; f ] -> Tuple6 (a, b, c, d, e, f)
| a :: b :: c :: d :: e :: f :: g :: l -> Many (a, b, c, d, e, f, g, l)
let fold_left ~f:fn ~init = function
| Tuple0 -> init
| Tuple1 a -> fn init a
| Tuple2 (a, b) -> fn (fn init a) b
| Tuple3 (a, b, c) -> fn (fn (fn init a) b) c
| Tuple4 (a, b, c, d) -> fn (fn (fn (fn init a) b) c) d
| Tuple5 (a, b, c, d, e) -> fn (fn (fn (fn (fn init a) b) c) d) e
| Tuple6 (a, b, c, d, e, f) -> fn (fn (fn (fn (fn (fn init a) b) c) d) e) f
| Many (a, b, c, d, e, f, g, l) ->
List.fold_left fn (fn (fn (fn (fn (fn (fn (fn init a) b) c) d) e) f) g) l