CCArray.find

This commit is contained in:
Simon Cruanes 2014-06-14 14:30:15 +02:00
parent 4c69e8216c
commit e2bb0e93cb
2 changed files with 12 additions and 0 deletions

View file

@ -57,6 +57,14 @@ let reverse_in_place a =
a = [| 6;5;4;3;2;1 |] 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 filter_map f a =
let rec aux acc i = let rec aux acc i =
if i = Array.length a if i = Array.length a

View file

@ -41,6 +41,10 @@ val filter : ('a -> bool) -> 'a t -> 'a t
val reverse_in_place : 'a t -> unit val reverse_in_place : 'a t -> unit
(** Reverse the array in place *) (** 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 val filter_map : ('a -> 'b option) -> 'a t -> 'b t
(** Map each element into another value, or discard it *) (** Map each element into another value, or discard it *)