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
|
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 *)
|
||||||
|
|
||||||
|
|
@ -115,7 +120,7 @@ module Idx : sig
|
||||||
val set : 'a t -> int -> 'a -> 'a t
|
val set : 'a t -> int -> 'a -> 'a t
|
||||||
(** set i-th element (removes the old one), or does nothing if
|
(** set i-th element (removes the old one), or does nothing if
|
||||||
index too high *)
|
index too high *)
|
||||||
|
|
||||||
val insert : 'a t -> int -> 'a -> 'a t
|
val insert : 'a t -> int -> 'a -> 'a t
|
||||||
(** insert at i-th position, between the two existing elements. If the
|
(** insert at i-th position, between the two existing elements. If the
|
||||||
index is too high, append at the end of the list *)
|
index is too high, append at the end of the list *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue