feat(CCArray): add optional argument eq to mem

This commit is contained in:
Fardale 2020-04-28 15:41:01 +02:00
parent 2025a62536
commit c50672ff7a
3 changed files with 20 additions and 1 deletions

View file

@ -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)

View file

@ -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].

View file

@ -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].