aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Utilities.ml
diff options
context:
space:
mode:
authorEstevan Castilho <tevo@riajuu.net>2026-08-23 03:17:08 +0000
committerEstevan Castilho <tevo@riajuu.net>2026-08-23 03:32:24 +0000
commit26e03cf21247aeed6e6f2bfc269c4c540ca9a451 (patch)
tree046a7b0ce8827a44621bcc727d39642fe19804b4 /lib/Utilities.ml
parent11e41f1179f870d25ed6621d488a8dca2d7005c2 (diff)
parent07c3ad6114efe808ee595f83f29dd0c79a6f2536 (diff)
downloadRNT-26e03cf21247aeed6e6f2bfc269c4c540ca9a451.tar.gz
RNT-26e03cf21247aeed6e6f2bfc269c4c540ca9a451.zip
Merge branch 'ocaml-rewrite' into new-ob
Diffstat (limited to 'lib/Utilities.ml')
-rw-r--r--lib/Utilities.ml73
1 files changed, 66 insertions, 7 deletions
diff --git a/lib/Utilities.ml b/lib/Utilities.ml
index 52483b3..e4ee9c3 100644
--- a/lib/Utilities.ml
+++ b/lib/Utilities.ml
@@ -7,14 +7,73 @@ end
module Result = struct
let ( let* ) = Result.bind
let fmap f m = Result.bind m f
+end
+
+module type SEQUENCE = sig
+ type 'a t
+
+ val empty : 'a t
+ val cons : 'a -> 'a t -> 'a t
+ val fold : ('a -> 'b -> 'b) -> 'a t -> 'b -> 'b
+end
+
+module Generic (S : SEQUENCE) = struct
+ let sequence ms =
+ let open Result in
+ S.fold
+ (fun m acc ->
+ let* acc = acc in
+ let* m = m in
+ Ok (S.cons m acc))
+ ms
+ (Ok S.empty)
+end
+
+module List = struct
+ module ListSequence : SEQUENCE with type 'a t = 'a list = struct
+ type 'a t = 'a list
+
+ let empty = []
+ let cons x xs = x::xs
+ let fold = List.fold_right
+ end
+
+ include Generic (ListSequence)
+end
+
+module FingerTree = struct
+ module FingerTreeSequence : SEQUENCE with type 'a t = 'a BatFingerTree.t = struct
+ type 'a t = 'a BatFingerTree.t
+
+ let empty = BatFingerTree.empty
+ let cons x xs = BatFingerTree.cons xs x
+ let fold f xs acc = BatFingerTree.fold_right
+ (Fun.flip f)
+ acc
+ xs
+ end
+
+ include Generic (FingerTreeSequence)
+
+ let map2 f xs ys =
+ let rec frob xs ys acc =
+ match BatFingerTree.front xs with
+ | None -> acc
+ | Some (xs', x) ->
+ match BatFingerTree.front ys with
+ | None -> acc
+ | Some (ys', y) ->
+ f x y
+ |> BatFingerTree.snoc acc
+ |> frob xs' ys'
+ in
+ frob xs ys BatFingerTree.empty
+
+ let zip xs ys = map2 (fun x y -> x, y) xs ys
- let sequence ms = List.fold_right
- (fun m acc ->
- let* acc = acc in
- let* m = m in
- Ok (m::acc))
- ms
- (Ok [])
+ let join ss sep =
+ BatFingerTree.print ~first:"" ~last:"" ~sep:sep BatIO.nwrite
+ |> Fun.flip BatIO.to_string ss
end
module Option = struct