add CCMap.union

This commit is contained in:
Simon Cruanes 2017-09-30 15:02:03 +02:00
parent e9b9ed1d92
commit 4096122979
2 changed files with 19 additions and 0 deletions

View file

@ -29,6 +29,11 @@ module type S = sig
(** [merge_safe ~f a b] merges the maps [a] and [b] together.
@since 0.17 *)
val union : (key -> 'a -> 'a -> 'a option) -> 'a t -> 'a t -> 'a t
(** Union of both maps, using the function to combine bindings
that belong to both inputs
@since NEXT_RELEASE *)
val of_seq : (key * 'a) sequence -> 'a t
val add_seq : 'a t -> (key * 'a) sequence -> 'a t
@ -85,6 +90,15 @@ module Make(O : Map.OrderedType) = struct
| Some v1, Some v2 -> f k (`Both (v1,v2)))
a b
let union f a b =
merge
(fun k v1 v2 -> match v1, v2 with
| None, None -> assert false
| None, (Some _ as r) -> r
| Some _ as r, None -> r
| Some v1, Some v2 -> f k v1 v2)
a b
let add_seq m s =
let m = ref m in
s (fun (k,v) -> m := add k v !m);

View file

@ -32,6 +32,11 @@ module type S = sig
(** [merge_safe ~f a b] merges the maps [a] and [b] together.
@since 0.17 *)
val union : (key -> 'a -> 'a -> 'a option) -> 'a t -> 'a t -> 'a t
(** Union of both maps, using the function to combine bindings
that belong to both inputs
@since NEXT_RELEASE *)
val of_seq : (key * 'a) sequence -> 'a t
(** Same as {!of_list} *)