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
let list_slow_append l1 l2 = List.rev_append (List.rev l1) l2
let rec list_count_append l1 l2 count =
match l2 with
| [] -> l1
| _ ->
match l1 with
| [] -> l2
| [x1] -> x1 :: l2
| [x1; x2] -> x1 :: x2 :: l2
| [x1; x2; x3] -> x1 :: x2 :: x3 :: l2
| [x1; x2; x3; x4] -> x1 :: x2 :: x3 :: x4 :: l2
| x1 :: x2 :: x3 :: x4 :: x5 :: tl ->
x1 :: x2 :: x3 :: x4 :: x5 ::
(if count > 1000
then list_slow_append tl l2
else list_count_append tl l2 (count + 1))
let list_append l1 l2 = list_count_append l1 l2 0
module Std =
struct
module List =
struct
include List
let map = Piqi_piqirun.list_map
let append = list_append
let concat l =
let rec aux accu = function
| [] -> rev accu
| h::t -> aux (rev_append h accu) t
in
aux [] l
let flatten = concat
let fold_right f l accu =
fold_left (fun a b -> f b a) accu (rev l)
let split l =
let rec aux accu_a accu_b = function
| [] ->
rev accu_a, rev accu_b
| (a,b)::t ->
aux (a::accu_a) (b::accu_b) t
in
aux [] [] l
let combine l1 l2 =
let rec aux accu l1 l2 =
match l1, l2 with
| [], [] -> rev accu
| (h1::t1), (h2::t2) ->
aux ((h1, h2)::accu) t1 t2
| (_, _) ->
invalid_arg "List.combine"
in
aux [] l1 l2
end
let ( @ ) = List.append
end
open Std
let flatmap f l =
let rec aux accu = function
| [] -> List.rev accu
| h::t -> aux (List.rev_append (f h) accu) t
in
aux [] l
let string_subst_char s x y =
if not (String.contains s x)
then s
else
let s = Bytes.of_string s in
for i = 0 to (Bytes.length s) - 1
do
if Bytes.get s i = x
then Bytes.set s i y
done;
Bytes.unsafe_to_string s
let list_of_string s =
let n = String.length s in
let rec aux i =
if i < n
then s.[i] :: (aux (i+1))
else []
in aux 0
let string_of_list l =
let s = Bytes.create (List.length l) in
let rec aux i = function
| [] -> ()
| h::t ->
Bytes.set s i h;
aux (i+1) t
in
aux 0 l;
Bytes.unsafe_to_string s
let dashes_to_underscores s =
string_subst_char s '-' '_'
let underscores_to_dashes s =
string_subst_char s '_' '-'
let string_split s ?(start=0) sep =
let rec aux len i accu =
if i < start
then
let name = String.sub s 0 (i+len+1) in
name::accu
else
let c = s.[i] in
if c = sep
then
let part = String.sub s (i + 1) len in
aux 0 (i - 1) (part :: accu)
else
aux (len + 1) (i - 1) accu
in
let len = String.length s in
if not (String.contains s sep) || len = 0
then [s]
else aux 0 (len - 1) []
let with_bool bool_ref value f =
let saved_value = !bool_ref in
bool_ref := value;
try
let res = f () in
bool_ref := saved_value;
res
with exn ->
bool_ref := saved_value;
raise exn
let find_dups l =
let rec aux = function
| [] -> None
| h::t ->
try
let dup = List.find (fun x -> x = h) t in
Some (dup, h)
with Not_found -> aux t
in aux l
let quote s = "\"" ^ s ^ "\""
let rec uniqq = function
| [] -> []
| h::t ->
let t = uniqq t in
if List.memq h t then t else h :: t
let uniqq l =
List.rev (uniqq (List.rev l))
let rec uniq = function
| [] -> []
| h::t ->
let t = uniq t in
if List.mem h t then t else h :: t
let uniq l =
List.rev (uniq (List.rev l))