mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -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 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 =
|
||||
|
|
@ -215,7 +233,7 @@ module MakeCounter(X : Hashtbl.HashedType) = struct
|
|||
let n = get tbl x in
|
||||
T.replace tbl x (n+1)
|
||||
|
||||
let incr_by tbl n x =
|
||||
let incr_by tbl n x =
|
||||
let n' = get tbl x in
|
||||
if n' + n <= 0
|
||||
then T.remove tbl x
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||
*)
|
||||
|
||||
|
||||
(** {1 Extension to the standard Hashtbl}
|
||||
(** {1 Extension to the standard Hashtbl}
|
||||
|
||||
@since 0.4 *)
|
||||
|
||||
|
|
@ -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 *)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue