Source file batSeq.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
##V>=5##module Pervasives = Stdlib
type 'a node =
##V>=4.7## 'a Stdlib.Seq.node =
| Nil
| Cons of 'a * 'a t
##V<4.7## and 'a t = unit -> 'a node
##V>=4.7## and 'a t = 'a Stdlib.Seq.t
type 'a mappable = 'a t
let nil () = Nil
let cons e s () = Cons(e, s)
let length s =
let rec aux acc s = match s () with
| Nil -> acc
| Cons(_, s) -> aux (acc + 1) s
in
aux 0 s
let rec enum_of_ref r =
BatEnum.make
~next:(fun _ -> match !r () with
| Nil ->
raise BatEnum.No_more_elements
| Cons(e, s) ->
r := s;
e)
~count:(fun _ -> length !r)
~clone:(fun _ -> enum_of_ref (ref !r))
let enum s = enum_of_ref (ref s)
let hd s = match s () with
| Nil -> invalid_arg "Seq.hd"
| Cons(e, _s) -> e
let tl s = match s () with
| Nil -> invalid_arg "Seq.tl"
| Cons(_e, s) -> s
let first s = match s () with
| Nil -> invalid_arg "Seq.first"
| Cons(e, _s) -> e
let last s =
let rec aux e s = match s () with
| Nil -> e
| Cons(e, s) -> aux e s
in
match s () with
| Nil -> invalid_arg "Seq.last"
| Cons(e, s) -> aux e s
let is_empty s = s () = Nil
let at s n =
let rec aux s n =
match s () with
| Nil -> invalid_arg "Seq.at"
| Cons(e, s) ->
if n = 0 then
e
else
aux s (n - 1)
in
if n < 0 then
invalid_arg "Seq.at"
else
aux s n
let rec append s1 s2 () = match s1 () with
| Nil -> s2 ()
| Cons(e, s1) -> Cons(e, append s1 s2)
let concat s =
let rec aux current rest () = match current () with
| Cons(e, s) ->
Cons(e, aux s rest)
| Nil ->
match rest () with
| Cons(e, s) ->
aux e s ()
| Nil ->
Nil
in
aux nil s
let flatten = concat
let make n e =
let rec aux n () =
if n = 0 then
Nil
else
Cons(e, aux (n - 1))
in
if n < 0 then
invalid_arg "Seq.make"
else
aux n
let init n f =
let rec aux i () =
if i = n then
Nil
else
Cons(f i, aux (i + 1))
in
if n < 0 then
invalid_arg "Seq.init"
else
aux 0
let of_list l =
let rec aux l () = match l with
| [] -> Nil
| x::l' -> Cons(x, aux l')
in
aux l
let empty = nil
let return x =
cons x empty
let rec unfold f u =
match f u with
| Some(v, r) -> (fun () -> Cons(v, unfold f r))
| None -> nil
let rec iter f s = match s () with
| Nil -> ()
| Cons(e, s) -> f e; iter f s
let iteri f s =
let rec iteri f i s = match s () with
| Nil -> ()
| Cons(e, s) -> f i e; iteri f (i+1) s
in iteri f 0 s
let rec iter2 f s1 s2 = match s1 (), s2 () with
| Nil, _
| _, Nil -> ()
| Cons (x1, s1'), Cons (x2, s2') -> f x1 x2; iter2 f s1' s2'
let rec map f s () = match s () with
| Nil -> Nil
| Cons(x, s) -> Cons(f x, map f s)
let flat_map f s =
flatten (map f s)
let concat_map =
flat_map
let mapi f s =
let rec mapi f i s () = match s () with
| Nil -> Nil
| Cons(x, s) -> Cons(f i x, mapi f (i+1) s)
in mapi f 0 s
let rec map2 f s1 s2 () = match s1 (), s2 () with
| Nil, _
| _, Nil -> Nil
| Cons (x1, s1'), Cons (x2, s2') ->
Cons (f x1 x2, map2 f s1' s2')
let rec fold_left f acc s = match s () with
| Nil -> acc
| Cons(e, s) -> fold_left f (f acc e) s
let rec fold_right f s acc = match s () with
| Nil -> acc
| Cons(e, s) -> f e (fold_right f s acc)
let reduce f s = match s () with
| Nil -> invalid_arg "Seq.reduce"
| Cons(e, s) -> fold_left f e s
let max s = match s () with
| Nil -> invalid_arg "Seq.max"
| Cons(e, s) -> fold_left Pervasives.max e s
let min s = match s () with
| Nil -> invalid_arg "Seq.min"
| Cons(e, s) -> fold_left Pervasives.min e s
let equal ?(eq=(=)) s1 s2 =
let rec recurse eq s1 s2 =
match s1 (), s2 () with
| Nil, Nil -> true
| Nil, Cons _
| Cons _, Nil -> false
| Cons (x1, s1'), Cons (x2, s2') -> eq x1 x2 && recurse eq s1' s2'
in
recurse eq s1 s2
let rec for_all f s = match s () with
| Nil -> true
| Cons(e, s) -> f e && for_all f s
let rec exists f s = match s () with
| Nil -> false
| Cons(e, s) -> f e || exists f s
let mem e s = exists ((=) e) s
let rec find f s = match s () with
| Nil ->
None
| Cons(e, s) ->
if f e then
Some e
else
find f s
let rec find_map f s = match s () with
| Nil ->
None
| Cons(e, s) ->
match f e with
| None ->
find_map f s
| x ->
x
let rec filter f s () = match s () with
| Nil ->
Nil
| Cons(e, s) ->
if f e then
Cons(e, filter f s)
else
filter f s ()
let rec filter_map f s () = match s () with
| Nil ->
Nil
| Cons(e, s) ->
match f e with
| None ->
filter_map f s ()
| Some e ->
Cons(e, filter_map f s)
let assoc key s = find_map (fun (k, v) -> if k = key then Some v else None) s
let rec take n s () =
if n <= 0 then
Nil
else
match s () with
| Nil ->
Nil
| Cons(e, s) ->
Cons(e, take (n - 1) s)
let rec drop n s =
if n <= 0 then
s
else
match s () with
| Nil ->
nil
| Cons(_e, s) ->
drop (n - 1) s
let rec take_while f s () = match s () with
| Nil ->
Nil
| Cons(e, s) ->
if f e then
Cons(e, take_while f s)
else
Nil
let rec drop_while f s = match s () with
| Nil ->
nil
| Cons(e, s) ->
if f e then
drop_while f s
else
cons e s
let split s = (map fst s, map snd s)
let rec combine s1 s2 () = match s1 (), s2 () with
| Nil, Nil ->
Nil
| Cons(e1, s1), Cons(e2, s2) ->
Cons((e1, e2), combine s1 s2)
| _ ->
invalid_arg "Seq.combine"
let print ?(first="[") ?(last="]") ?(sep="; ") print_a out s = match s () with
| Nil ->
BatInnerIO.nwrite out first;
BatInnerIO.nwrite out last
| Cons(e, s) ->
match s () with
| Nil ->
BatPrintf.fprintf out "%s%a%s" first print_a e last
| _ ->
BatInnerIO.nwrite out first;
print_a out e;
iter (BatPrintf.fprintf out "%s%a" sep print_a) s;
BatInnerIO.nwrite out last
let to_buffer ?(first="[") ?(last="]") ?(sep=";") to_str buff s =
match s () with
| Nil -> (Buffer.add_string buff first;
Buffer.add_string buff last)
| Cons(e, s) ->
match s () with
| Nil -> (Buffer.add_string buff first;
Buffer.add_string buff (to_str e);
Buffer.add_string buff last)
| _ ->
Buffer.add_string buff first;
Buffer.add_string buff (to_str e);
iter (fun e ->
Buffer.add_string buff sep;
Buffer.add_string buff (to_str e)
) s;
Buffer.add_string buff last
let to_string ?(first="[") ?(last="]") ?(sep=";") to_str s =
let buff = Buffer.create 80 in
to_buffer ~first ~last ~sep to_str buff s;
Buffer.contents buff
let of_string ?(first="[") ?(last="]") ?(sep=";") of_str s =
if not (BatString.starts_with s first) then
raise
(Invalid_argument
("Seq.of_string: wrong prefix: " ^ first ^ " not prefix of " ^ s));
if not (BatString.ends_with s last) then
raise
(Invalid_argument
("Seq.of_string: wrong suffix: " ^ last ^ " not suffix of " ^ s));
let prfx_len = String.length first in
let sufx_len = String.length last in
let n = String.length s in
if n = prfx_len + sufx_len then nil
else
let body = BatString.chop ~l:prfx_len ~r:sufx_len s in
let strings = BatString.nsplit ~by:sep body in
of_list (List.map of_str strings)
##V>=4.14##let uncons = Stdlib.Seq.uncons
##V>=4.14##let fold_lefti = Stdlib.Seq.fold_lefti
##V>=4.14##let fold_left2 = Stdlib.Seq.fold_left2
##V>=4.14##let for_all2 = Stdlib.Seq.for_all2
##V>=4.14##let exists2 = Stdlib.Seq.exists2
##V>=4.14##let compare = Stdlib.Seq.compare
##V>=4.14##let repeat = Stdlib.Seq.repeat
##V>=4.14##let forever = Stdlib.Seq.forever
##V>=4.14##let cycle = Stdlib.Seq.cycle
##V>=4.14##let iterate = Stdlib.Seq.iterate
##V>=4.14##let scan = Stdlib.Seq.scan
##V>=4.14##let group = Stdlib.Seq.group
##V>=4.14##let memoize = Stdlib.Seq.memoize
##V>=4.14##exception Forced_twice = Stdlib.Seq.Forced_twice
##V>=4.14##let once = Stdlib.Seq.once
##V>=4.14##let transpose = Stdlib.Seq.transpose
##V>=4.14##let zip = Stdlib.Seq.zip
##V>=4.14##let interleave = Stdlib.Seq.interleave
##V>=4.14##let sorted_merge = Stdlib.Seq.sorted_merge
##V>=4.14##let product = Stdlib.Seq.product
##V>=4.14##let map_product = Stdlib.Seq.map_product
##V>=4.14##let unzip = Stdlib.Seq.unzip
##V>=4.14##let partition_map = Stdlib.Seq.partition_map
##V>=4.14##let partition = Stdlib.Seq.partition
##V>=4.14##let of_dispenser = Stdlib.Seq.of_dispenser
##V>=4.14##let to_dispenser = Stdlib.Seq.to_dispenser
##V>=4.14##let ints = Stdlib.Seq.ints
##V>=4.14##let equal_stdlib = Stdlib.Seq.equal
##V>=5.1##let find_index = Stdlib.Seq.find_index
##V>=5.1##let find_mapi = Stdlib.Seq.find_mapi
module Infix = struct
(** Infix operators matching those provided by {!BatEnum.Infix} *)
let ( -- ) a b =
if b < a then
nil
else
init (b - a + 1) (fun x -> a + x)
let ( --^ ) a b = a -- (b - 1)
let ( --. ) (a, step) b =
let n = int_of_float ((b -. a) /. step) + 1 in
if n < 0 then
nil
else
init n (fun i -> float_of_int i *. step +. a)
let ( --- ) a b =
let n = abs (b - a) in
if b < a then
init n (fun x -> a - x)
else
a -- b
let ( --~ ) a b =
map Char.chr (Char.code a -- Char.code b)
let ( // ) s f = filter f s
let ( /@ ) s f = map f s
let ( @/ ) = map
let ( //@ ) s f = filter_map f s
let ( @// ) = filter_map
end
include Infix
module Exceptionless = struct
let hd s =
try Some (hd s)
with Invalid_argument _ -> None
let tl s =
try Some (tl s)
with Invalid_argument _ -> None
let first s =
try Some (first s)
with Invalid_argument _ -> None
let last s =
try Some (last s)
with Invalid_argument _ -> None
let at s n =
try Some (at s n)
with Invalid_argument _ -> None
let reduce f s =
try Some (reduce f s)
with Invalid_argument _ -> None
let max s =
try Some (max s)
with Invalid_argument _ -> None
let min s =
try Some (min s)
with Invalid_argument _ -> None
let rec combine s1 s2 () = match s1 (), s2 () with
| Nil, Nil ->
Nil
| Cons(e1, s1), Cons(e2, s2) ->
Cons((e1, e2), combine s1 s2)
| _ ->
Nil
end