add CCList.{find_pred,find_pred_exn}

This commit is contained in:
Simon Cruanes 2015-05-17 19:18:40 +02:00
parent fc7f1a661a
commit 153cababb3
2 changed files with 26 additions and 0 deletions

View file

@ -385,6 +385,22 @@ let last n l =
let len = List.length l in
if len < n then l else drop (len-n) l
let rec find_pred p l = match l with
| [] -> None
| x :: _ when p x -> Some x
| _ :: tl -> find_pred p tl
let find_pred_exn p l = match find_pred p l with
| None -> raise Not_found
| Some x -> x
(*$T
find_pred ((=) 4) [1;2;5;4;3;0] = Some 4
find_pred (fun _ -> true) [] = None
find_pred (fun _ -> false) (1 -- 10) = None
find_pred (fun x -> x < 10) (1 -- 9) = Some 1
*)
let findi f l =
let rec aux f i = function
| [] -> None

View file

@ -109,6 +109,16 @@ val last : int -> 'a t -> 'a t
(** [last n l] takes the last [n] elements of [l] (or less if
[l] doesn't have that many elements *)
val find_pred : ('a -> bool) -> 'a t -> 'a option
(** [find_pred p l] finds the first element of [l] that satisfies [p],
or returns [None] if no element satisfies [p]
@since NEXT_RELEASE *)
val find_pred_exn : ('a -> bool) -> 'a t -> 'a
(** Unsafe version of {!find_pred}
@raise Not_found if no such element is found
@since NEXT_RELEASE *)
val find : ('a -> 'b option) -> 'a t -> 'b option
(** [find f l] traverses [l], applying [f] to each element. If for
some element [x], [f x = Some y], then [Some y] is returned. Otherwise