mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-08 04:05:30 -05:00
api change in CClist: find is now find_idx, and find:('a->'b option) -> 'a list -> 'b option added
This commit is contained in:
parent
0461758bf9
commit
ef06d117cb
2 changed files with 20 additions and 3 deletions
|
|
@ -219,13 +219,25 @@ let last n l =
|
||||||
let len = List.length l in
|
let len = List.length l in
|
||||||
if len < n then l else drop (len-n) l
|
if len < n then l else drop (len-n) l
|
||||||
|
|
||||||
let find p l =
|
let find_idx p l =
|
||||||
let rec search i l = match l with
|
let rec search i l = match l with
|
||||||
| [] -> None
|
| [] -> None
|
||||||
| x::_ when p x -> Some (i, x)
|
| x::_ when p x -> Some (i, x)
|
||||||
| _::xs -> search (i+1) xs
|
| _::xs -> search (i+1) xs
|
||||||
in search 0 l
|
in search 0 l
|
||||||
|
|
||||||
|
let rec find f l = match l with
|
||||||
|
| [] -> None
|
||||||
|
| x::l' ->
|
||||||
|
match f x with
|
||||||
|
| Some _ as res -> res
|
||||||
|
| None -> find f l'
|
||||||
|
|
||||||
|
(*$T
|
||||||
|
find (fun x -> if x=3 then Some "a" else None) [1;2;3;4] = Some "a"
|
||||||
|
find (fun x -> if x=3 then Some "a" else None) [1;2;4;5] = None
|
||||||
|
*)
|
||||||
|
|
||||||
let filter_map f l =
|
let filter_map f l =
|
||||||
let rec recurse acc l = match l with
|
let rec recurse acc l = match l with
|
||||||
| [] -> List.rev acc
|
| [] -> List.rev acc
|
||||||
|
|
|
||||||
|
|
@ -86,10 +86,15 @@ val last : int -> 'a t -> 'a t
|
||||||
(** [last n l] takes the last [n] elements of [l] (or less if
|
(** [last n l] takes the last [n] elements of [l] (or less if
|
||||||
[l] doesn't have that many elements *)
|
[l] doesn't have that many elements *)
|
||||||
|
|
||||||
val find : ('a -> bool) -> 'a t -> (int * 'a) option
|
val find_idx : ('a -> bool) -> 'a t -> (int * 'a) option
|
||||||
(** [find p x] returns [Some (i,x)] where [x] is the [i]-th element of [l],
|
(** [find p x] returns [Some (i,x)] where [x] is the [i]-th element of [l],
|
||||||
and [p x] holds. Otherwise returns [None] *)
|
and [p x] holds. Otherwise returns [None] *)
|
||||||
|
|
||||||
|
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
|
||||||
|
the call returns [None] *)
|
||||||
|
|
||||||
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
|
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
|
||||||
(** Map and remove elements at the same time *)
|
(** Map and remove elements at the same time *)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue