mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
merge from master
This commit is contained in:
commit
f25c363851
2 changed files with 20 additions and 3 deletions
|
|
@ -219,13 +219,25 @@ let last n l =
|
|||
let len = List.length l in
|
||||
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
|
||||
| [] -> None
|
||||
| x::_ when p x -> Some (i, x)
|
||||
| _::xs -> search (i+1) xs
|
||||
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 rec recurse acc l = match l with
|
||||
| [] -> 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
|
||||
[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],
|
||||
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
|
||||
(** Map and remove elements at the same time *)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue