From 2025a625362a675aa32f3bd43fe93d421be303dc Mon Sep 17 00:00:00 2001 From: Fardale Date: Tue, 28 Apr 2020 15:39:47 +0200 Subject: [PATCH] feat(CCList): make mem compatible with the Stdlib The eq argument is now optional. --- src/core/CCList.ml | 9 +++++++-- src/core/CCList.mli | 6 +++--- src/core/CCListLabels.mli | 6 +++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 9d2672dc..70ab4f5b 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -1215,12 +1215,17 @@ let group_join_by (type a) ?(eq=Stdlib.(=)) ?(hash=Hashtbl.hash) f c1 c2 = (Error "e2") (all_ok [Ok 1; Error "e2"; Error "e3"; Ok 4]) *) -let mem ~eq x l = +let mem ?(eq=Stdlib.(=)) x l = let rec search eq x l = match l with | [] -> false | y::l' -> eq x y || search eq x l' in search eq x l +(*$Q mem + Q.(small_list small_int) (fun l -> \ + mem 1 l = (List.mem 1 l)) +*) + let add_nodup ~eq x l = if mem ~eq x l then l else x::l @@ -1545,7 +1550,7 @@ module Assoc = struct = [1, "1"; 2, "2"; 3, "3"] *) - let mem ~eq x l = + let mem ?(eq=Stdlib.(=)) x l = try ignore (search_exn eq l x); true with Not_found -> false diff --git a/src/core/CCList.mli b/src/core/CCList.mli index 6f18433d..c1cd6eb4 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -544,7 +544,7 @@ val remove_one : eq:('a -> 'a -> bool) -> 'a -> 'a t -> 'a t (** [remove_one x set] removes one occurrence of [x] from [set]. Linear time. @since 0.11 *) -val mem : eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool +val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool (** Membership to the list. Linear time. *) val subset : eq:('a -> 'a -> bool) -> 'a t -> 'a t -> bool @@ -607,7 +607,7 @@ module Assoc : sig val set : eq:('a->'a->bool) -> 'a -> 'b -> ('a,'b) t -> ('a,'b) t (** Add the binding into the list (erase it if already present). *) - val mem : eq:('a->'a->bool) -> 'a -> ('a,_) t -> bool + val mem : ?eq:('a->'a->bool) -> 'a -> ('a,_) t -> bool (** [mem x l] returns [true] iff [x] is a key in [l]. @since 0.16 *) @@ -637,7 +637,7 @@ val assq_opt : 'a -> ('a * 'b) t -> 'b option @since 1.5, but only @since 2.0 with labels *) -val mem_assoc : eq:('a -> 'a -> bool) -> 'a -> ('a * _) t -> bool +val mem_assoc : ?eq:('a -> 'a -> bool) -> 'a -> ('a * _) t -> bool (** Like [Assoc.mem]. @since 2.0 *) diff --git a/src/core/CCListLabels.mli b/src/core/CCListLabels.mli index b9371d7d..714a3acf 100644 --- a/src/core/CCListLabels.mli +++ b/src/core/CCListLabels.mli @@ -548,7 +548,7 @@ val remove_one : eq:(('a -> 'a -> bool) [@keep_label]) -> 'a -> 'a t -> 'a t (** [remove_one x set] removes one occurrence of [x] from [set]. Linear time. @since 0.11 *) -val mem : eq:(('a -> 'a -> bool) [@keep_label]) -> 'a -> 'a t -> bool +val mem : ?eq:(('a -> 'a -> bool) [@keep_label]) -> 'a -> 'a t -> bool (** Membership to the list. Linear time. *) val subset : eq:(('a -> 'a -> bool) [@keep_label]) -> 'a t -> 'a t -> bool @@ -611,7 +611,7 @@ module Assoc : sig val set : eq:(('a->'a->bool) [@keep_label]) -> 'a -> 'b -> ('a,'b) t -> ('a,'b) t (** Add the binding into the list (erase it if already present). *) - val mem : eq:(('a->'a->bool) [@keep_label]) -> 'a -> ('a,_) t -> bool + val mem : ?eq:(('a->'a->bool) [@keep_label]) -> 'a -> ('a,_) t -> bool (** [mem x l] returns [true] iff [x] is a key in [l]. @since 0.16 *) @@ -641,7 +641,7 @@ val assq_opt : 'a -> ('a * 'b) t -> 'b option @since 1.5, but only @since 2.0 with labels *) -val mem_assoc : eq:(('a -> 'a -> bool) [@keep_label]) -> 'a -> ('a * _) t -> bool +val mem_assoc : ?eq:(('a -> 'a -> bool) [@keep_label]) -> 'a -> ('a * _) t -> bool (** Like [Assoc.mem]. @since 2.0 *)