mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
CCArray.find
This commit is contained in:
parent
4c69e8216c
commit
e2bb0e93cb
2 changed files with 12 additions and 0 deletions
|
|
@ -57,6 +57,14 @@ let reverse_in_place a =
|
|||
a = [| 6;5;4;3;2;1 |]
|
||||
*)
|
||||
|
||||
let find f a =
|
||||
let rec find i =
|
||||
if i = Array.length a then None
|
||||
else match f a.(i) with
|
||||
| Some _ as res -> res
|
||||
| None -> find (i+1)
|
||||
in find 0
|
||||
|
||||
let filter_map f a =
|
||||
let rec aux acc i =
|
||||
if i = Array.length a
|
||||
|
|
|
|||
|
|
@ -41,6 +41,10 @@ val filter : ('a -> bool) -> 'a t -> 'a t
|
|||
val reverse_in_place : 'a t -> unit
|
||||
(** Reverse the array in place *)
|
||||
|
||||
val find : ('a -> 'b option) -> 'a t -> 'b option
|
||||
(** [find f a] returns [Some y] if there is an element [x] such
|
||||
that [f x = Some y], else it returns [None] *)
|
||||
|
||||
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
|
||||
(** Map each element into another value, or discard it *)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue