Source file tablecloth.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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
let ( <| ) a b = a b
let ( >> ) (f1 : 'a -> 'b) (f2 : 'b -> 'c) : 'a -> 'c = fun x -> x |> f1 |> f2
let ( << ) (f1 : 'b -> 'c) (f2 : 'a -> 'b) : 'a -> 'c = fun x -> x |> f2 |> f1
let identity (value : 'a) : 'a = value
module Array = struct
let empty : 'a array = [||]
let singleton (a : 'a) : 'a array = [|a|]
let length (a : 'a array) : int = Base.Array.length a
let isEmpty (a : 'a array) : bool = length a = 0
let is_empty = isEmpty
let initialize ~(length : int) ~(f : int -> 'a) =
if length <= 0 then empty else Base.Array.init length ~f
let repeat ~(length : int) (e : 'a) : 'a array =
if length <= 0 then empty else Base.Array.init length ~f:(fun _ -> e)
let range ?(from = 0) (to_ : int) : int array =
Base.Array.init (max 0 (to_ - from)) ~f:(fun i -> i + from)
let fromList (l: 'a list) : 'a array = Base.List.to_array l
let from_list = fromList
let toList (a: 'a array) : 'a list = Base.Array.to_list a
let to_list = toList
let toIndexedList xs =
Base.Array.fold_right xs ~init:(length xs - 1, []) ~f:(fun x (i, acc) ->
(i - 1, ((i, x) :: acc)))
|> Base.snd
let to_indexed_list = toIndexedList
let get ~index a =
if index >= 0 && index < length a then Some (Base.Array.get a index) else None
let set ~index ~value a = Base.Array.set a index value
let sum (a : int array) : int = Base.Array.fold a ~init:0 ~f:( + )
let floatSum (a : float array) : float = Base.Array.fold a ~init:0.0 ~f:( +. )
let float_sum = floatSum
let filter ~(f : 'a -> bool) (a : 'a array) : 'a array = Base.Array.filter a ~f
let map ~(f : 'a -> 'b) (a : 'a array) : 'b array = Base.Array.map a ~f
let mapWithIndex ~(f : 'int -> 'a -> 'b) (a : 'a array) : 'b array = Base.Array.mapi a ~f
let map_with_index = mapWithIndex
let mapi = mapWithIndex
let map2 ~(f : 'a -> 'b -> 'c) (a : 'a array) (b : 'b array) : 'c array =
let minLength = min (length a) (length b) in
Base.Array.init minLength ~f:(fun i -> f a.(i) b.(i))
let map3 ~(f : 'a -> 'b -> 'c -> 'd) (arrayA : 'a array) (arrayB : 'b array) (arrayC : 'c array) : 'd array =
let minLength : int = Base.Array.fold ~f:Base.min ~init:(length arrayA) [|length arrayB; length arrayC|] in
Base.Array.init minLength ~f:(fun i -> f arrayA.(i) arrayB.(i) arrayC.(i))
let flatMap ~f a = Base.Array.concat_map a ~f
let flat_map = flatMap
let find ~(f : 'a -> bool) (a : 'a array) : 'a option = Base.Array.find a ~f
let append (a : 'a array) (a' : 'a array) : 'a array = Base.Array.append a a'
let concatenate (al : 'a array array) : 'a array = Base.Array.concat (Base.Array.to_list al)
let intersperse ~sep array =
Base.Array.init (max 0 (Array.length array * 2 - 1)) ~f:(fun i ->
if i mod 2 <> 0 then sep else array.(i / 2)
)
let any ~(f : 'a -> bool) (a : 'a array) : bool = Base.Array.exists ~f a
let all ~(f : 'a -> bool) (a : 'a array) : bool = Base.Array.for_all ~f a
let slice ~from ?to_ array =
let defaultTo = match to_ with
| None -> length array
| Some i -> i
in
let sliceFrom =
if from >= 0 then min (length array) from
else max 0 (min (length array) (length array + from))
in
let sliceTo =
if defaultTo >= 0 then min (length array) defaultTo
else max 0 (min (length array) (length array + defaultTo))
in
if sliceFrom >= sliceTo then empty else (
Base.Array.init (sliceTo - sliceFrom) ~f:(fun i -> array.(i + sliceFrom))
)
let foldLeft ~(f : 'a -> 'b -> 'b) ~(initial : 'b) (a : 'a array) : 'b =
Base.Array.fold ~f:(fun b a -> f a b) ~init:initial a
let fold_left = foldLeft
let foldRight ~(f : 'a -> 'b -> 'b) ~(initial : 'b) (a : 'a array) : 'b =
Base.Array.fold_right ~f ~init:initial a
let fold_right = foldRight
let reverse (a : 'a array) : 'a array =
let copy = Base.Array.copy a in
Base.Array.rev_inplace copy;
copy
let reverseInPlace (a : 'a array) = Base.Array.rev_inplace a
let reverse_in_place = reverseInPlace
let forEach ~(f : 'a -> unit) (a : 'a array) : unit = Base.Array.iter a ~f
let for_each = forEach
end
module Tuple2 = struct
let create a b = (a, b)
let first ((a, _) : 'a * 'b) : 'a = a
let second ((_, b) : 'a * 'b) : 'b = b
let mapFirst ~(f : 'a -> 'x) ((a, b) : 'a * 'b) : 'x * 'b = (f a, b)
let map_first = mapFirst
let mapSecond ~(f : 'b -> 'y) ((a, b) : 'a * 'b) : 'a * 'y = (a, f b)
let map_second = mapSecond
let mapEach ~(f : 'a -> 'x) ~(g : 'b -> 'y) ((a, b) : 'a * 'b) : 'x * 'y = (f a, g b)
let map_each = mapEach
let mapAll ~(f : 'a -> 'b) (a1, a2) = (f a1, f a2)
let map_all = mapAll
let swap (a, b) = (b, a)
let curry (f : (('a * 'b) -> 'c)) (a : 'a) (b : 'b) : 'c = f (a, b)
let uncurry (f : ('a -> 'b -> 'c)) ((a, b) : ('a * 'b)) : 'c = f a b
let toList (a, b) = [a; b]
let to_list = toList
end
module Tuple3 = struct
let create a b c = (a, b, c)
let first ((a, _, _) : 'a * 'b * 'c) : 'a = a
let second ((_, b, _) : 'a * 'b * 'c) : 'b = b
let third ((_, _, c) : 'a * 'b * 'c) : 'c = c
let init ((a, b, _) : 'a * 'b * 'c): ('a * 'b) = (a, b)
let tail ((_, b, c) : 'a * 'b * 'c): ('b * 'c) = (b, c)
let mapFirst ~(f : 'a -> 'x) ((a, b, c) : 'a * 'b * 'c) : 'x * 'b *'c = (f a, b, c)
let map_first = mapFirst
let mapSecond ~(f : 'b -> 'y) ((a, b, c) : 'a * 'b * 'c) : 'a * 'y * 'c = (a, f b, c)
let map_second = mapSecond
let mapThird ~(f : 'c -> 'z) ((a, b, c) : 'a * 'b * 'c) : 'a * 'b * 'z = (a, b, f c)
let map_third = mapThird
let mapEach ~(f : 'a -> 'x) ~(g : 'b -> 'y) ~(h : 'c -> 'z) ((a, b, c) : 'a * 'b * 'c) : 'x * 'y * 'z = (f a, g b, h c)
let map_each = mapEach
let mapAll ~(f: 'a -> 'b) (a1, a2, a3) = (f a1, f a2, f a3)
let map_all = mapAll
let rotateLeft ((a, b, c) : 'a * 'b * 'c) : ('b * 'c * 'a) = (b, c, a)
let rotate_left = rotateLeft
let rotateRight ((a, b, c) : 'a * 'b * 'c) : ('c * 'a * 'b) = (c, a, b)
let rotate_right = rotateRight
let curry (f : (('a * 'b * 'c) -> 'd)) (a : 'a) (b : 'b) (c : 'c) : 'd = f (a, b, c)
let uncurry (f : 'a -> 'b -> 'c -> 'd) ((a, b, c) : ('a * 'b * 'c)) : 'd = f a b c
let toList ((a, b, c) : ('a * 'a * 'a)) : 'a list = [a; b; c]
let to_list = toList
end
module List = struct
let flatten = Base.List.concat
let sum (l : int list) : int =
Base.List.reduce l ~f:( + ) |> Base.Option.value ~default:0
let floatSum (l : float list) : float =
Base.List.reduce l ~f:( +. ) |> Base.Option.value ~default:0.0
let float_sum = floatSum
let map ~(f : 'a -> 'b) (l : 'a list) : 'b list = Base.List.map l ~f
let indexedMap ~(f : 'int -> 'a -> 'b) (l : 'a list) : 'b list =
Base.List.mapi l ~f
let indexed_map = indexedMap
let mapi = indexedMap
let map2 ~(f : 'a -> 'b -> 'c) (a : 'a list) (b : 'b list) : 'c list =
Base.List.map2_exn a b ~f
let getBy ~(f : 'a -> bool) (l : 'a list) : 'a option = Base.List.find l ~f
let get_by = getBy
let find = getBy
let elemIndex ~(value : 'a) (l : 'a list) : int option =
Base.List.findi l ~f:(fun _ v -> v = value)
|> Base.Option.map ~f:Tuple2.first
let elem_index = elemIndex
let rec last (l : 'a list) : 'a option =
match l with [] -> None | [a] -> Some a | _ :: tail -> last tail
let member ~(value : 'a) (l : 'a list) : bool =
Base.List.exists l ~f:(( = ) value)
let uniqueBy ~(f : 'a -> string) (l : 'a list) : 'a list =
let rec uniqueHelp
~(f : 'a -> string)
(existing : Base.Set.M(Base.String).t)
(remaining : 'a list)
(accumulator : 'a list) : 'a list =
match remaining with
| [] ->
List.rev accumulator
| first :: rest ->
let computedFirst = f first in
if Base.Set.mem existing computedFirst
then uniqueHelp ~f existing rest accumulator
else
uniqueHelp
~f
(Base.Set.add existing computedFirst)
rest
(first :: accumulator)
in
uniqueHelp ~f (Base.Set.empty (module Base.String)) l []
let unique_by = uniqueBy
let getAt ~(index : int) (l : 'a list) : 'a option = Base.List.nth l index
let get_at = getAt
let any ~(f : 'a -> bool) (l : 'a list) : bool = List.exists f l
let head (l : 'a list) : 'a option = Base.List.hd l
let drop ~(count : int) (l : 'a list) : 'a list = Base.List.drop l count
let init (l : 'a list) : 'a list option =
match List.rev l with _ :: rest -> Some (List.rev rest) | [] -> None
let filterMap ~(f : 'a -> 'b option) (l : 'a list) : 'b list =
Base.List.filter_map l ~f
let filter_map = filterMap
let filter ~(f : 'a -> bool) (l : 'a list) : 'a list = Base.List.filter l ~f
let concat (ls : 'a list list) : 'a list = Base.List.concat ls
let partition ~(f : 'a -> bool) (l : 'a list) : 'a list * 'a list =
Base.List.partition_tf ~f l
let foldr ~(f : 'a -> 'b -> 'b) ~(init : 'b) (l : 'a list) : 'b =
List.fold_right f l init
let foldl ~(f : 'a -> 'b -> 'b) ~(init : 'b) (l : 'a list) : 'b =
List.fold_right f (List.rev l) init
let rec findIndexHelp
(index : int) ~(predicate : 'a -> bool) (list : 'a list) : int option =
match list with
| [] ->
None
| x :: xs ->
if predicate x
then Some index
else findIndexHelp (index + 1) ~predicate xs
let findIndex ~(f : 'a -> bool) (l : 'a list) : int option =
findIndexHelp 0 ~predicate:f l
let find_index = findIndex
let take ~(count : int) (l : 'a list) : 'a list = Base.List.take l count
let updateAt ~(index : int) ~(f : 'a -> 'a) (list : 'a list) : 'a list =
if index < 0
then list
else
let head = take ~count:index list in
let tail = drop ~count:index list in
match tail with x :: xs -> head @ (f x :: xs) | _ -> list
let update_at = updateAt
let length (l : 'a list) : int = List.length l
let reverse (l : 'a list) : 'a list = List.rev l
let rec dropWhile ~(f : 'a -> bool) (list : 'a list) : 'a list =
match list with
| [] ->
[]
| x :: xs ->
if f x then dropWhile ~f xs else list
let drop_while = dropWhile
let isEmpty (l : 'a list) : bool = l = []
let is_empty = isEmpty
let cons (item : 'a) (l : 'a list) : 'a list = item :: l
let takeWhile ~(f : 'a -> bool) (l : 'a list) : 'a list =
let rec takeWhileMemo memo list =
match list with
| [] ->
List.rev memo
| x :: xs ->
if f x then takeWhileMemo (x :: memo) xs else List.rev memo
in
takeWhileMemo [] l
let take_while = takeWhile
let all ~(f : 'a -> bool) (l : 'a list) : bool = Base.List.for_all l ~f
let tail (l : 'a list) : 'a list option =
match l with [] -> None | _ :: rest -> Some rest
let append (l1 : 'a list) (l2 : 'a list) : 'a list = l1 @ l2
let removeAt ~(index : int) (l : 'a list) : 'a list =
if index < 0
then l
else
let head = take ~count:index l in
let tail = drop ~count:index l |> tail in
match tail with None -> l | Some t -> append head t
let remove_at = removeAt
let minimumBy ~(f : 'a -> 'comparable) (ls : 'a list) : 'a option =
let minBy x (y, fy) =
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 <| foldl ~f:minBy ~init:(l1, f l1) lrest)
| _ ->
None
let minimum_by = minimumBy
let minimum (list : 'comparable list) : 'comparable option =
match list with x :: xs -> Some (foldl ~f:min ~init:x xs) | _ -> None
let maximumBy ~(f : 'a -> 'comparable) (ls : 'a list) : 'a option =
let maxBy x (y, fy) =
let fx = f x in
if fx > fy then (x, fx) else (y, fy)
in
match ls with
| [l_] ->
Some l_
| l_ :: ls_ ->
Some (fst <| foldl ~f:maxBy ~init:(l_, f l_) ls_)
| _ ->
None
let maximum_by = maximumBy
let maximum (list : 'comparable list) : 'comparable option =
match list with x :: xs -> Some (foldl ~f:max ~init:x xs) | _ -> None
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 span ~(f : 'a -> bool) (xs : 'a list) : 'a list * 'a list =
(takeWhile ~f xs, dropWhile ~f xs)
let rec groupWhile ~(f : 'a -> 'a -> bool) (xs : 'a list) : 'a list list =
match xs with
| [] ->
[]
| x :: xs ->
let ys, zs = span ~f:(f x) xs in
(x :: ys) :: groupWhile ~f zs
let group_while = groupWhile
let splitAt ~(index : int) (xs : 'a list) : 'a list * 'a list =
(take ~count:index xs, drop ~count:index xs)
let split_at = splitAt
let insertAt ~(index : int) ~(value : 'a) (xs : 'a list) : 'a list =
take ~count:index xs @ (value :: drop ~count:index xs)
let insert_at = insertAt
let splitWhen ~(f : 'a -> bool) (l : 'a list) : ('a list * 'a list) =
match findIndex ~f l with
| Some index -> splitAt ~index l
| None -> (l, [])
let split_when = splitWhen
let intersperse (sep : 'a) (xs : 'a list) : 'a list =
match xs with
| [] ->
[]
| hd :: tl ->
let step x rest = sep :: x :: rest in
let spersed = foldr ~f:step ~init:[] tl in
hd :: spersed
let initialize (n : int) (f : int -> 'a) : 'a list =
let rec step i acc = if i < 0 then acc else step (i - 1) (f i :: acc) in
step (n - 1) []
let sortWith (f : 'a -> 'a -> int) (l : 'a list) : 'a list =
Base.List.sort l ~compare:f
let sort_with = sortWith
let iter ~(f : 'a -> unit) (l : 'a list) : unit = List.iter f l
end
module Option = struct
type 'a t = 'a option
let some = Base.Option.some
let andThen ~(f : 'a -> 'b option) (o : 'a option) : 'b option =
match o with None -> None | Some x -> f x
let and_then = andThen
let or_ (ma : 'a option) (mb : 'a option) : 'a option =
match ma with None -> mb | Some _ -> ma
let orElse (ma : 'a option) (mb : 'a option) : 'a option =
match mb with None -> ma | Some _ -> mb
let or_else = orElse
let map ~(f : 'a -> 'b) (o : 'a option) : 'b option = Base.Option.map o ~f
let withDefault ~(default : 'a) (o : 'a option) : 'a =
Base.Option.value o ~default
let with_default = withDefault
let values (l : 'a option list) : 'a list =
let valuesHelper (item : 'a option) (list : 'a list) : 'a list =
match item with None -> list | Some v -> v :: list in
List.foldr ~f:valuesHelper ~init:[] l
let toList (o : 'a option) : 'a list =
match o with None -> [] | Some o -> [o]
let to_list = toList
let isSome = Base.Option.is_some
let is_some = isSome
let toOption ~(sentinel : 'a) (value : 'a) : 'a option =
if value = sentinel then None else Some value
let to_option = toOption
end
module Result = struct
type ('err, 'ok) t = ('ok, 'err) Base.Result.t
let succeed = Base.Result.return
let fail = Base.Result.fail
let withDefault ~(default : 'ok) (r : ('err, 'ok) t) : 'ok =
Base.Result.ok r |> Base.Option.value ~default
let with_default = withDefault
let map2 ~(f : 'a -> 'b -> 'c) (a : ('err, 'a) t) (b : ('err, 'b) t) :
('err, 'c) t =
match (a, b) with
| Ok a, Ok b ->
Ok (f a b)
| Error a, Ok _ ->
Error a
| Ok _, Error b ->
Error b
| Error a, Error _ ->
Error a
let combine (l : ('x, 'a) t list) : ('x, 'a list) t =
List.foldr ~f:(map2 ~f:(fun a b -> a :: b)) ~init:(Ok []) l
let map (f : 'ok -> 'value) (r : ('err, 'ok) t) : ('err, 'value) t =
Base.Result.map r ~f
let toOption (r : ('err, 'ok) t) : 'ok option =
match r with Ok v -> Some v | _ -> None
let to_option = toOption
let andThen ~(f : 'ok -> ('err, 'value) t) (r : ('err, 'ok) t) :
('err, 'value) t =
Base.Result.bind ~f r
let and_then = andThen
let pp
(errf : Format.formatter -> 'err -> unit)
(okf : Format.formatter -> 'ok -> unit)
(fmt : Format.formatter)
(r : ('err, 'ok) t) =
match r with
| Ok ok ->
Format.pp_print_string fmt "<ok: " ;
okf fmt ok ;
Format.pp_print_string fmt ">"
| Error err ->
Format.pp_print_string fmt "<error: " ;
errf fmt err ;
Format.pp_print_string fmt ">"
end
module Char = struct
let toCode (c : char) : int = Base.Char.to_int c
let to_code = toCode
let fromCode (i : int) : char option =
if 0 <= i && i <= 255 then Some (Char.chr i) else None
let from_code = fromCode
let toString = Base.Char.to_string
let to_string = toString
let fromString (str : string) : char option = match String.length str with
| 1 -> Some (String.get str 0)
| _ -> None
let from_string = fromString
let toDigit char = match char with
| '0'..'9' -> Some (toCode char - toCode '0')
| _ -> None
let to_digit = toDigit
let toLowercase = Base.Char.lowercase
let to_lowercase = toLowercase
let toUppercase = Base.Char.uppercase
let to_uppercase = toUppercase
let isLowercase = Base.Char.is_lowercase
let is_lowercase = isLowercase
let isUppercase = Base.Char.is_uppercase
let is_uppercase = isUppercase
let isLetter = Base.Char.is_alpha
let is_letter = isLetter
let isDigit = Base.Char.is_digit
let is_digit = isDigit
let isAlphanumeric = Base.Char.is_alphanum
let is_alphanumeric = isAlphanumeric
let isPrintable = Base.Char.is_print
let is_printable = isPrintable
let isWhitespace = Base.Char.is_whitespace
let is_whitespace = isWhitespace
end
module Int = struct
let negate = (~-)
let isEven n = n mod 2 = 0
let is_even = isEven
let isOdd n = n mod 2 != 0
let is_odd = isOdd
end
module String = struct
let length = String.length
let toInt (s : string) : (string, int) Result.t =
try Ok (int_of_string s) with e -> Error (Printexc.to_string e)
let to_int = toInt
let toFloat (s : string) : (string, float) Result.t =
try Ok (float_of_string s) with e -> Error (Printexc.to_string e)
let to_float = toFloat
let uncons (s : string) : (char * string) option =
match s with
| "" ->
None
| s ->
Some (s.[0], String.sub s 1 (String.length s - 1))
let dropLeft ~(count : int) (s : string) : string =
Base.String.drop_prefix s count
let drop_left = dropLeft
let dropRight ~(count : int) (s : string) : string =
Base.String.drop_suffix s count
let drop_right = dropRight
let split ~(on : string) (s : string) : string list =
let on = Str.regexp_string on in
Str.split on s
let join ~(sep : string) (l : string list) : string = String.concat sep l
let endsWith ~(suffix : string) (s : string) =
Base.String.is_suffix ~suffix s
let ends_with = endsWith
let startsWith ~(prefix : string) (s : string) =
Base.String.is_prefix ~prefix s
let starts_with = startsWith
let toLower (s : string) : string = String.lowercase_ascii s
let to_lower = toLower
let toUpper (s : string) : string = String.uppercase_ascii s
let to_upper = toUpper
let uncapitalize (s : string) : string = String.uncapitalize_ascii s
let capitalize (s : string) : string = String.capitalize_ascii s
let isCapitalized (s : string) : bool = s = String.capitalize_ascii s
let is_capitalized = isCapitalized
let contains ~(substring : string) (s : string) : bool =
Base.String.is_substring s ~substring
let repeat ~(count : int) (s : string) : string =
Base.List.init count ~f:(fun _ -> s) |> Base.String.concat
let reverse (s : string) = Base.String.rev s
let fromList (l : char list) : string = Base.String.of_char_list l
let from_list = fromList
let toList (s : string) : char list = Base.String.to_list s
let to_list = toList
let fromInt (i : int) : string = string_of_int i
let from_int = fromInt
let concat = String.concat ""
let fromChar (c : char) : string = Base.String.of_char c
let from_char = fromChar
let slice ~from ~to_ str = String.sub str from (to_ - from)
let trim = String.trim
let insertAt ~(insert : string) ~(index : int) (s : string) : string =
let length = String.length s in
let startCount = index in
let endCount = length - index in
let start = dropRight ~count:endCount s in
let end_ = dropLeft ~count:startCount s in
join ~sep:"" [start; insert; end_]
let insert_at = insertAt
end
module IntSet = struct
module Set = Base.Set.M (Base.Int)
let __pp_value = Format.pp_print_int
type t = Set.t
type value = int
let fromList (l : value list) : t = Base.Set.of_list (module Base.Int) l
let from_list = fromList
let member ~(value : value) (s : t) : bool = Base.Set.mem s value
let diff (set1 : t) (set2 : t) : t = Base.Set.diff set1 set2
let isEmpty (s : t) : bool = Base.Set.is_empty s
let is_empty = isEmpty
let toList (s : t) : value list = Base.Set.to_list s
let to_list = toList
let ofList (s : value list) : t = Base.Set.of_list (module Base.Int) s
let of_list = ofList
let union (s1 : t) (s2 : t) : t = Base.Set.union s1 s2
let empty = Base.Set.empty (module Base.Int)
let add ~(value : value) (s : t) : t = Base.Set.add s value
let remove ~(value : value) (set : t) = Base.Set.remove set value
let set ~(value : value) (set : t) = add ~value set
let has = member
let pp (fmt : Format.formatter) (set : t) =
Format.pp_print_string fmt "{ " ;
Base.Set.iter set ~f:(fun v ->
__pp_value fmt v ;
Format.pp_print_string fmt ", " ) ;
Format.pp_print_string fmt " }" ;
()
end
module StrSet = struct
module Set = Base.Set.M (Base.String)
let __pp_value = Format.pp_print_string
type t = Set.t
type value = string
let fromList (l : value list) : t = Base.Set.of_list (module Base.String) l
let from_list = fromList
let member ~(value : value) (set : t) : bool = Base.Set.mem set value
let diff (set1 : t) (set2 : t) : t = Base.Set.diff set1 set2
let isEmpty (s : t) : bool = Base.Set.is_empty s
let is_empty = isEmpty
let toList (s : t) : value list = Base.Set.to_list s
let to_list = toList
let ofList (s : value list) : t = Base.Set.of_list (module Base.String) s
let of_list = ofList
let add ~(value : value) (s : t) : t = Base.Set.add s value
let union (s1 : t) (s2 : t) : t = Base.Set.union s1 s2
let empty = Base.Set.empty (module Base.String)
let remove ~(value : value) (set : t) = Base.Set.remove set value
let set ~(value : value) (set : t) = add ~value set
let has = member
let pp (fmt : Format.formatter) (set : t) =
Format.pp_print_string fmt "{ " ;
Base.Set.iter set ~f:(fun v ->
__pp_value fmt v ;
Format.pp_print_string fmt ", " ) ;
Format.pp_print_string fmt " }" ;
()
end
module StrDict = struct
module Map = Base.Map.M (Base.String)
type key = string
type 'value t = 'value Map.t
let toList t : ('key * 'value) list = Base.Map.to_alist t
let to_list = toList
let empty : 'value t = Base.Map.empty (module Base.String)
let fromList (l : ('key * 'value) list) : 'value t =
Base.Map.of_alist_reduce (module Base.String) ~f:(fun _ r -> r) l
let from_list = fromList
let get ~(key : key) (dict : 'value t) : 'value option =
Base.Map.find dict key
let insert ~(key : key) ~(value : 'value) (dict : 'value t) : 'value t =
Base.Map.set dict ~key ~data:value
let keys dict : key list = Base.Map.keys dict
let update ~(key : key) ~(f : 'v option -> 'v option) (dict : 'value t) :
'value t =
Base.Map.change dict key ~f
let map dict ~(f : 'a -> 'b) = Base.Map.map dict ~f
let pp
(valueFormatter : Format.formatter -> 'value -> unit)
(fmt : Format.formatter)
(map : 'value t) =
Format.pp_print_string fmt "{ " ;
Base.Map.iteri map ~f:(fun ~key ~data ->
Format.pp_print_string fmt key ;
Format.pp_print_string fmt ": " ;
valueFormatter fmt data ;
Format.pp_print_string fmt ", " ) ;
Format.pp_print_string fmt "}" ;
()
let merge
~(f : key -> 'v1 option -> 'v2 option -> 'v3 option)
(dict1 : 'v1 t)
(dict2 : 'v2 t) : 'v3 t =
Base.Map.merge dict1 dict2 ~f:(fun ~key desc ->
match desc with
| `Left v1 ->
f key (Some v1) None
| `Right v2 ->
f key None (Some v2)
| `Both (v1, v2) ->
f key (Some v1) (Some v2) )
end
module IntDict = struct
module Map = Base.Map.M (Base.Int)
type key = int
type 'value t = 'value Map.t
let toList t : ('key * 'value) list = Base.Map.to_alist t
let to_list = toList
let empty : 'value t = Base.Map.empty (module Base.Int)
let fromList (l : ('key * 'value) list) : 'value t =
Base.Map.of_alist_reduce (module Base.Int) ~f:(fun _ r -> r) l
let from_list = fromList
let get ~(key : key) (dict : 'value t) : 'value option =
Base.Map.find dict key
let insert ~(key : key) ~(value : 'value) (dict : 'value t) : 'value t =
Base.Map.set dict ~key ~data:value
let keys dict : key list = Base.Map.keys dict
let update ~(key : key) ~(f : 'v option -> 'v option) (dict : 'value t) :
'value t =
Base.Map.change dict key ~f
let map dict ~(f : 'a -> 'b) = Base.Map.map dict ~f
let pp
(valueFormatter : Format.formatter -> 'value -> unit)
(fmt : Format.formatter)
(map : 'value t) =
Format.pp_print_string fmt "{ " ;
Base.Map.iteri map ~f:(fun ~key ~data ->
Format.pp_print_int fmt key ;
Format.pp_print_string fmt ": " ;
valueFormatter fmt data ;
Format.pp_print_string fmt ", " ) ;
Format.pp_print_string fmt "}" ;
()
let merge
~(f : key -> 'v1 option -> 'v2 option -> 'v3 option)
(dict1 : 'v1 t)
(dict2 : 'v2 t) : 'v3 t =
Base.Map.merge dict1 dict2 ~f:(fun ~key desc ->
match desc with
| `Left v1 ->
f key (Some v1) None
| `Right v2 ->
f key None (Some v2)
| `Both (v1, v2) ->
f key (Some v1) (Some v2) )
end