From b42d12cfc0b663085bd5ae9e5e2d491e727e731e Mon Sep 17 00:00:00 2001 From: Marcos Magueta Date: Sat, 31 Oct 2026 02:55:39 -0300 Subject: range on the interface fold, stopping at the first error --- lib/kernel/Merkle.ml | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'lib/kernel/Merkle.ml') diff --git a/lib/kernel/Merkle.ml b/lib/kernel/Merkle.ml index b8599d5..be4618b 100644 --- a/lib/kernel/Merkle.ml +++ b/lib/kernel/Merkle.ml @@ -37,6 +37,15 @@ module type TREE = functor (S : Abstract.Storage.STORAGE) (K : KEY) -> sig val remove : S.transaction -> K.t -> node -> (node, Concepts.Condition.condition) result val lookup : S.transaction -> K.t -> node -> (address option, Concepts.Condition.condition) result + val foldm_left : + ?from:K.t -> + ?until:(K.t -> bool) -> + S.transaction -> + ('c -> K.t -> address -> ('c, Concepts.Condition.condition) result) -> + 'c -> + node -> + ('c, Concepts.Condition.condition) result + val fold_left : ?from:K.t -> ?until:(K.t -> bool) -> @@ -349,19 +358,19 @@ functor let drop i ft = if i < BatFingerTree.size ft then snd (BatFingerTree.split_at ft i) else BatFingerTree.empty - let fold_left ?from ?(until = fun _ -> false) tx f acc node = + let foldm_left ?from ?(until = fun _ -> false) tx f acc node = let open Utilities.Result in let rec walk from acc = function | Leaf {keys; values} -> let i = Option.fold ~none:0 ~some:(fun key -> fst (lookup1 keys key)) from in Utilities.FingerTree.zip (drop i keys) (drop i values) - |> BatFingerTree.fold_left + |> Utilities.FingerTree.foldm_left (fun step (k, v) -> match step with - | `Continue acc -> if until k then `Stop acc else `Continue (f acc k v) - | `Stop _ -> step ) + | `Continue acc when until k -> Ok (`Stop acc) + | `Continue acc -> f acc k v |> Result.map (fun acc -> `Continue acc) + | `Stop _ -> Ok step ) (`Continue acc) - |> Result.ok | Trunk {keys; children} -> let i = Option.fold ~none:0 @@ -384,6 +393,8 @@ functor in walk from acc node |> Result.map (function `Continue acc | `Stop acc -> acc) + let fold_left ?from ?until tx f = foldm_left ?from ?until tx (fun acc k v -> Ok (f acc k v)) + let keys tx node = fold_left tx (fun acc k _ -> BatFingerTree.snoc acc k) BatFingerTree.empty node @@ -459,6 +470,8 @@ module type INTERFACE = functor (S : Abstract.Storage.STORAGE) (K : KEY) (V : VA val mem : S.transaction -> K.t -> node -> (bool, Concepts.Condition.condition) result val fold_left : + ?from:K.t -> + ?until:(K.t -> bool) -> S.transaction -> ('c -> K.t -> V.t -> 'c) -> 'c -> @@ -518,14 +531,8 @@ functor let mem tx k node = Tree.lookup tx k node |> Result.map Option.is_some - let fold_left tx f acc node = - Tree.fold_left tx - (fun acc k addr -> - let* acc = acc in - let* v = retrieve tx addr in - Ok (f acc k v) ) - (Ok acc) node - |> Result.join + let fold_left ?from ?until tx f = + Tree.foldm_left ?from ?until tx (fun acc k addr -> retrieve tx addr |> Result.map (f acc k)) let iter tx f node = fold_left tx (fun _ k v -> f k v |> ignore) () node let keys = Tree.keys -- cgit v1.2.3