CCHashtbl.map_list

This commit is contained in:
Simon Cruanes 2014-12-17 10:22:27 +01:00
parent d0af6abbd9
commit 352b7e3901
2 changed files with 26 additions and 2 deletions

View file

@ -40,6 +40,16 @@ let keys tbl k = Hashtbl.iter (fun key _ -> k key) tbl
let values tbl k = Hashtbl.iter (fun _ v -> k v) tbl
let map_list f h =
Hashtbl.fold
(fun x y acc -> f x y :: acc)
h []
(*$T
of_list [1,"a"; 2,"b"] |> map_list (fun x y -> string_of_int x ^ y) \
|> List.sort Pervasives.compare = ["1a"; "2b"]
*)
let to_seq tbl k = Hashtbl.iter (fun key v -> k (key,v)) tbl
let of_seq seq =
@ -71,6 +81,9 @@ module type S = sig
val values : 'a t -> 'a sequence
(** Iterate on values in the table *)
val map_list : (key -> 'a -> 'b) -> 'a t -> 'b list
(** Map on a hashtable's items, collect into a list *)
val to_seq : 'a t -> (key * 'a) sequence
(** Iterate on values in the table *)
@ -95,6 +108,11 @@ module Make(X : Hashtbl.HashedType) = struct
let values tbl k = iter (fun _ v -> k v) tbl
let map_list f h =
fold
(fun x y acc -> f x y :: acc)
h []
let to_seq tbl k = iter (fun key v -> k (key,v)) tbl
let of_seq seq =

View file

@ -44,6 +44,9 @@ val keys : ('a,'b) Hashtbl.t -> 'a sequence
val values : ('a,'b) Hashtbl.t -> 'b sequence
(** Iterate on values in the table *)
val map_list : ('a -> 'b -> 'c) -> ('a, 'b) Hashtbl.t -> 'c list
(** Map on a hashtable's items, collect into a list *)
val to_seq : ('a,'b) Hashtbl.t -> ('a * 'b) sequence
(** Iterate on bindings in the table *)
@ -70,6 +73,9 @@ module type S = sig
val values : 'a t -> 'a sequence
(** Iterate on values in the table *)
val map_list : (key -> 'a -> 'b) -> 'a t -> 'b list
(** Map on a hashtable's items, collect into a list *)
val to_seq : 'a t -> (key * 'a) sequence
(** Iterate on values in the table *)