add CCVector.find_map

This commit is contained in:
Simon Cruanes 2015-10-21 14:15:33 +02:00
parent f952541044
commit 799089659a
2 changed files with 25 additions and 1 deletions

View file

@ -454,7 +454,9 @@ let find_exn p v =
let n = v.size in let n = v.size in
let rec check i = let rec check i =
if i = n then raise Not_found if i = n then raise Not_found
else if p v.vec.(i) then v.vec.(i) else
let x = v.vec.(i) in
if p x then x
else check (i+1) else check (i+1)
in check 0 in check 0
@ -462,6 +464,23 @@ let find p v =
try Some (find_exn p v) try Some (find_exn p v)
with Not_found -> None with Not_found -> None
let find_map f v =
let n = v.size in
let rec search i =
if i=n then None
else match f v.vec.(i) with
| None -> search (i+1)
| Some _ as res -> res
in
search 0
(*$Q
Q.(list small_int) (fun l -> \
let v = of_list l in \
let f x = x>30 && x < 35 in \
find_map (fun x -> if f x then Some x else None) v = find f v)
*)
let filter_map f v = let filter_map f v =
let v' = create () in let v' = create () in
iter iter

View file

@ -164,6 +164,11 @@ val find_exn : ('a -> bool) -> ('a,_) t -> 'a
(** find an element that satisfies the predicate, or (** find an element that satisfies the predicate, or
@raise Not_found if no element does *) @raise Not_found if no element does *)
val find_map : ('a -> 'b option) -> ('a,_) t -> 'b option
(** [find_map f v] returns the first [Some y = f x] for [x] in [v],
or [None] if [f x = None] for each [x] in [v]
@since NEXT_RELEASE *)
val filter_map : ('a -> 'b option) -> ('a,_) t -> ('b, 'mut) t val filter_map : ('a -> 'b option) -> ('a,_) t -> ('b, 'mut) t
(** Map elements with a function, possibly filtering some of them out *) (** Map elements with a function, possibly filtering some of them out *)