diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 0d23f6b9..9d32ba8d 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -793,15 +793,15 @@ let repeat i l = module Assoc = struct type ('a, 'b) t = ('a*'b) list - let get_exn ?(eq=(=)) l x = - let rec search eq l x = match l with - | [] -> raise Not_found - | (y,z)::l' -> - if eq x y then z else search eq l' x - in search eq l x + let rec search_exn eq l x = match l with + | [] -> raise Not_found + | (y,z)::l' -> + if eq x y then z else search_exn eq l' x - let get ?eq l x = - try Some (get_exn ?eq l x) + let get_exn ?(eq=(=)) l x = search_exn eq l x + + let get ?(eq=(=)) l x = + try Some (search_exn eq l x) with Not_found -> None (*$T @@ -826,6 +826,15 @@ module Assoc = struct Assoc.set [1,"1"; 2, "2"] 3 "3" |> List.sort Pervasives.compare \ = [1, "1"; 2, "2"; 3, "3"] *) + + let mem ?(eq=(=)) l x = + try ignore (search_exn eq l x); true + with Not_found -> false + + (*$T + Assoc.mem [1,"1"; 2,"2"; 3, "3"] 1 + not (Assoc.mem [1,"1"; 2,"2"; 3, "3"] 4) + *) end (** {2 Zipper} *) diff --git a/src/core/CCList.mli b/src/core/CCList.mli index bbb5ce2e..e2b04501 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -300,6 +300,10 @@ module Assoc : sig val set : ?eq:('a->'a->bool) -> ('a,'b) t -> 'a -> 'b -> ('a,'b) t (** Add the binding into the list (erase it if already present) *) + + val mem : ?eq:('a->'a->bool) -> ('a,_) t -> 'a -> bool + (** [mem l x] returns [true] iff [x] is a key in [l] + @since NEXT_RELEASE *) end (** {2 Zipper} *)