mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 03:35:30 -05:00
CCHashtbl.map_list
This commit is contained in:
parent
d0af6abbd9
commit
352b7e3901
2 changed files with 26 additions and 2 deletions
|
|
@ -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 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 to_seq tbl k = Hashtbl.iter (fun key v -> k (key,v)) tbl
|
||||||
|
|
||||||
let of_seq seq =
|
let of_seq seq =
|
||||||
|
|
@ -71,6 +81,9 @@ module type S = sig
|
||||||
val values : 'a t -> 'a sequence
|
val values : 'a t -> 'a sequence
|
||||||
(** Iterate on values in the table *)
|
(** 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
|
val to_seq : 'a t -> (key * 'a) sequence
|
||||||
(** Iterate on values in the table *)
|
(** 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 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 to_seq tbl k = iter (fun key v -> k (key,v)) tbl
|
||||||
|
|
||||||
let of_seq seq =
|
let of_seq seq =
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,9 @@ val keys : ('a,'b) Hashtbl.t -> 'a sequence
|
||||||
val values : ('a,'b) Hashtbl.t -> 'b sequence
|
val values : ('a,'b) Hashtbl.t -> 'b sequence
|
||||||
(** Iterate on values in the table *)
|
(** 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
|
val to_seq : ('a,'b) Hashtbl.t -> ('a * 'b) sequence
|
||||||
(** Iterate on bindings in the table *)
|
(** Iterate on bindings in the table *)
|
||||||
|
|
||||||
|
|
@ -70,6 +73,9 @@ module type S = sig
|
||||||
val values : 'a t -> 'a sequence
|
val values : 'a t -> 'a sequence
|
||||||
(** Iterate on values in the table *)
|
(** 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
|
val to_seq : 'a t -> (key * 'a) sequence
|
||||||
(** Iterate on values in the table *)
|
(** Iterate on values in the table *)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue