diff --git a/src/core/CCList.ml b/src/core/CCList.ml index aa777a27..23fb0dec 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -1814,6 +1814,12 @@ module Assoc = struct [1,"1"; 2,"2"] \ (Assoc.remove ~eq:CCInt.equal 3 [1,"1"; 2,"2"] |> lsort) *) + + let keys l = map (fun (k, _) -> k) l + + let values l = map (fun (_, v) -> v) l + + let map_values f l = map (fun (k, v) -> (k, f v)) l end let assoc = Assoc.get_exn diff --git a/src/core/CCList.mli b/src/core/CCList.mli index bda8c72b..0ef6dbcf 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -747,6 +747,18 @@ module Assoc : sig val remove : eq:('a->'a->bool) -> 'a -> ('a,'b) t -> ('a,'b) t (** [remove ~eq k alist] returns the [alist] without the first pair with key [k], if any. @since 0.17 *) + + val keys : ('a, 'b) t -> 'a list + (** [keys alist] returns a list of all keys of [alist]. + @since 3.8 *) + + val values : ('a, 'b) t -> 'b list + (** [values alist] returns a list of all values of [alist]. + @since 3.8 *) + + val map_values : ('b -> 'c) -> ('a, 'b) t -> ('a, 'c) t + (** [map_values f alist] applies function [f] to all values of [alist]. + @since 3.8 *) end val assoc : eq:('a -> 'a -> bool) -> 'a -> ('a * 'b) t -> 'b diff --git a/src/core/CCListLabels.mli b/src/core/CCListLabels.mli index 6b85dfdf..2b6e3ffc 100644 --- a/src/core/CCListLabels.mli +++ b/src/core/CCListLabels.mli @@ -711,6 +711,19 @@ module Assoc : sig val remove : eq:(('a->'a->bool) [@keep_label]) -> 'a -> ('a,'b) t -> ('a,'b) t (** [remove ~eq k alist] returns the [alist] without the first pair with key [k], if any. @since 0.17 *) + + val keys : ('a, 'b) t -> 'a list + (** [keys alist] returns a list of all keys of [alist]. + @since 3.8 *) + + val values : ('a, 'b) t -> 'b list + (** [values alist] returns a list of all values of [alist]. + @since 3.8 *) + + val map_values : ('b -> 'c) -> ('a, 'b) t -> ('a, 'c) t + (** [map_values f alist] applies function [f] to all values of [alist]. + @since 3.8 *) + end val assoc : eq:(('a -> 'a -> bool) [@keep_label]) -> 'a -> ('a * 'b) t -> 'b