Commits

Anonymous committed 6b3242b

map_rw: + fold

  • Participants
  • Parent commits 80ac3f1

Comments (0)

Files changed (1)

         method get_exn : 'k -> 'v;
         method get_opt : 'k -> option 'v;
         method mem : 'k -> bool;
+        (* no methods like iter/fold, because map can be
+          just a function or even dns-lookup, so we don't know
+          or should not know the keys.
+          either:
+            - change map_ro to morphism for not-knowing-the-key, or
+            - change map_ro to collection_ro for these who know its keys
+         *)
       end
     ;
 
         method add : 'k -> 'v -> map_rw 'k 'v;
         method remove : 'k -> map_rw 'k 'v;
         method replace : 'k -> 'v -> map_rw 'k 'v;
+
+        method fold : !'a . ('a -> 'k -> 'v -> 'a) -> 'a -> 'a;
+
       end
     ;
 
         method add : 'k -> 'v -> unit;
         method remove : 'k -> unit;
         method replace : 'k -> 'v -> unit;
+        method fold : !'a . ('a -> 'k -> 'v -> 'a) -> 'a -> 'a;
       end
     ;
 
           let added = Cd_list.map_rw_assoc_add removed k v in
           new map_rw_assoc ~keq added
         ;
+        method fold : !'a. ('a -> 'k -> 'v -> 'a) -> 'a -> 'a = fun f a ->
+          let rec inner a lst =
+            match lst with
+            [ [] -> a
+            | [(hk, hv) :: t] -> inner (f a hk hv) t
+            ]
+          in
+            inner a cur
+        ;
       end
     ;
 
              value mem cur k = M.mem k cur;
              value add cur k v = M.add k v cur;
              value remove cur k = M.remove k cur;
+             value fold cur f a = M.fold
+               (fun k v a -> f a k v)
+               cur
+               a
+             ;
            end
         ;
 
               let added = Cd_tree.add removed k v in
               new map_rw_tree_cur added
             ;
+            method fold : !'a. ('a -> 'k -> 'v -> 'a) -> 'a -> 'a = fun f a ->
+              Cd_tree.fold cur f a
+            ;
           end
         ;
 
           cur := cur#replace k v
         ;
 
+        method fold : !'a. ('a -> 'k -> 'v -> 'a) -> 'a -> 'a =
+          cur#fold
+        ;
+
       end
     ;