mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-07 11:45:31 -05:00
feat(CCArray): add optional argument eq to mem
This commit is contained in:
parent
2025a62536
commit
c50672ff7a
3 changed files with 20 additions and 1 deletions
|
|
@ -197,8 +197,21 @@ let rev a =
|
||||||
rev [| |] = [| |]
|
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 =
|
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
|
else match f i a.(i) with
|
||||||
| Some _ as res -> res
|
| Some _ as res -> res
|
||||||
| None -> find_aux f a (i+1)
|
| None -> find_aux f a (i+1)
|
||||||
|
|
|
||||||
|
|
@ -132,6 +132,9 @@ val sort_ranking : ('a -> 'a -> int) -> 'a t -> int array
|
||||||
[lookup_exn a.(i) (sorted a) = (sorted_ranking a).(i)].
|
[lookup_exn a.(i) (sorted a) = (sorted_ranking a).(i)].
|
||||||
@since 1.0 *)
|
@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
|
val find_map : ('a -> 'b option) -> 'a t -> 'b option
|
||||||
(** [find_map f a] returns [Some y] if there is an element [x] such
|
(** [find_map f a] returns [Some y] if there is an element [x] such
|
||||||
that [f x = Some y]. Otherwise returns [None].
|
that [f x = Some y]. Otherwise returns [None].
|
||||||
|
|
|
||||||
|
|
@ -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)].
|
[lookup_exn a.(i) (sorted a) = (sorted_ranking a).(i)].
|
||||||
@since 1.0 *)
|
@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
|
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
|
(** [find_map ~f a] returns [Some y] if there is an element [x] such
|
||||||
that [~f x = Some y]. Otherwise returns [None].
|
that [~f x = Some y]. Otherwise returns [None].
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue