1. Yit Phang Khoo
  2. Adapton.ocaml

Commits

Yit Phang Khoo  committed 6906cc2

Add SAList.memo_{filter_map,map_with_key}.

  • Participants
  • Parent commits 4f01bc3
  • Branches default

Comments (0)

Files changed (2)

File Source/Adapton/SAList.ml

View file
  • Ignore whitespace
                 | `Nil -> `Nil
             end
 
+        (** Create memoizing constructor and updater that simultaneously filter and map a self-adjusting list with a predicate/mapping function. *)
+        let memo_filter_map (type a) (type b) (module L : Signatures.SAListType.BasicS with type sa = sa and type data = a and type t = b) f =
+            memo (module L) begin fun filter xs -> match L.force xs with
+                | `Cons ( x, xs ) -> (match f x with Some y -> `Cons ( y, filter xs ) | None -> force (filter xs))
+                | `Nil -> `Nil
+            end
+
         (** Create memoizing constructor and updater that map a self-adjusting list with a mapping function. *)
         let memo_map (type a) (type b) (module L : Signatures.SAListType.BasicS with type sa = sa and type data = a and type t = b) f =
             memo (module L) begin fun map xs -> match L.force xs with
                 | `Nil -> `Nil
             end
 
+        (** Create memoizing constructor and updater that map a self-adjusting list with a mapping function and key. *)
+        let memo_map_with_key
+                (type a) (module K : Hashtbl.SeededHashedType with type t = a)
+                (type b) (type c) (module L : Signatures.SAListType.BasicS with type sa = sa and type data = b and type t = c)
+                f =
+            memo2 (module K) (module L) begin fun map k xs -> match L.force xs with
+                | `Cons ( x, xs ) -> `Cons ( f k x, map k xs )
+                | `Nil -> `Nil
+            end
+
         (** Create memoizing constructor and updater that scan (fold over prefixes of) a self-adjusting list with an scanning function. *)
         let memo_scan (type a) (type b) (module L : Signatures.SAListType.BasicS with type sa = sa and type data = a and type t = b) f =
             memo2 (module L) (module R) begin fun scan xs acc -> match L.force xs with

File Source/Adapton/Signatures.ml

View file
  • Ignore whitespace
         val pop : t -> data
         val memo_append : (t -> t -> t) * (t -> t -> t -> unit)
         val memo_filter : (data -> bool) -> (t -> t) * (t -> t -> unit)
+        val memo_filter_map
+            : (module SAListType.BasicS with type sa = sa and type data = 'a and type t = 'b)
+                -> ('a -> data option) -> ('b -> t) * (t -> 'b -> unit)
         val memo_map
             : (module SAListType.BasicS with type sa = sa and type data = 'a and type t = 'b)
                 -> ('a -> data) -> ('b -> t) * (t -> 'b -> unit)
+        val memo_map_with_key
+            : (module Hashtbl.SeededHashedType with type t = 'a)
+                -> (module SAListType.BasicS with type sa = sa and type data = 'b and type t = 'c)
+                -> ('a -> 'b -> data) -> ('a -> 'c -> t) * (t -> 'a -> 'c -> unit)
         val memo_scan
             : (module SAListType.BasicS with type sa = sa and type data = 'a and type t = 'b)
                 -> ('a -> data -> data) -> ('b -> data -> t) * (t -> 'b -> data -> unit)