mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
add CCVector.find_map
This commit is contained in:
parent
f952541044
commit
799089659a
2 changed files with 25 additions and 1 deletions
|
|
@ -454,7 +454,9 @@ let find_exn p v =
|
|||
let n = v.size in
|
||||
let rec check i =
|
||||
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)
|
||||
in check 0
|
||||
|
||||
|
|
@ -462,6 +464,23 @@ let find p v =
|
|||
try Some (find_exn p v)
|
||||
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 v' = create () in
|
||||
iter
|
||||
|
|
|
|||
|
|
@ -164,6 +164,11 @@ val find_exn : ('a -> bool) -> ('a,_) t -> 'a
|
|||
(** find an element that satisfies the predicate, or
|
||||
@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
|
||||
(** Map elements with a function, possibly filtering some of them out *)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue