diff --git a/src/core/CCArray.ml b/src/core/CCArray.ml index 2d593617..ac237245 100644 --- a/src/core/CCArray.ml +++ b/src/core/CCArray.ml @@ -197,8 +197,21 @@ let rev a = rev [| |] = [| |] *) +exception Found + +let mem ?(eq = Stdlib.(=)) elt a = + try + Array.iter (fun e -> if eq e elt then raise Found) a; + false + with Found -> true + +(*$Q mem + Q.(array small_int) (fun a -> \ + mem 1 a = (Array.mem 1 a)) +*) + let rec find_aux f a i = - if i = Array.length a then None + if i >= Array.length a then None else match f i a.(i) with | Some _ as res -> res | None -> find_aux f a (i+1) diff --git a/src/core/CCArray.mli b/src/core/CCArray.mli index cb6d4bb0..f254a8e6 100644 --- a/src/core/CCArray.mli +++ b/src/core/CCArray.mli @@ -132,6 +132,9 @@ val sort_ranking : ('a -> 'a -> int) -> 'a t -> int array [lookup_exn a.(i) (sorted a) = (sorted_ranking a).(i)]. @since 1.0 *) +val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool +(** [mem ~eq x a] return true if x is present in [a]. Linear time. *) + val find_map : ('a -> 'b option) -> 'a t -> 'b option (** [find_map f a] returns [Some y] if there is an element [x] such that [f x = Some y]. Otherwise returns [None]. diff --git a/src/core/CCArrayLabels.mli b/src/core/CCArrayLabels.mli index af596800..bd23893b 100644 --- a/src/core/CCArrayLabels.mli +++ b/src/core/CCArrayLabels.mli @@ -131,6 +131,9 @@ val sort_ranking : f:('a -> 'a -> int) -> 'a t -> int array [lookup_exn a.(i) (sorted a) = (sorted_ranking a).(i)]. @since 1.0 *) +val mem : ?eq:('a -> 'a -> bool) -> 'a -> 'a t -> bool +(** [mem ~eq x a] return true if x is present in [a]. Linear time. *) + val find_map : f:('a -> 'b option) -> 'a t -> 'b option (** [find_map ~f a] returns [Some y] if there is an element [x] such that [~f x = Some y]. Otherwise returns [None].