mirror of
https://github.com/c-cube/iter.git
synced 2025-12-06 11:15:32 -05:00
add find_map{,i} as better alias to existing functions
This commit is contained in:
parent
ffe157a1c6
commit
a41a997c8f
2 changed files with 15 additions and 3 deletions
|
|
@ -699,7 +699,7 @@ let mem ?(eq=(=)) x seq = exists (eq x) seq
|
||||||
|
|
||||||
exception ExitFind
|
exception ExitFind
|
||||||
|
|
||||||
let find f seq =
|
let find_map f seq =
|
||||||
let r = ref None in
|
let r = ref None in
|
||||||
begin
|
begin
|
||||||
try
|
try
|
||||||
|
|
@ -711,7 +711,9 @@ let find f seq =
|
||||||
end;
|
end;
|
||||||
!r
|
!r
|
||||||
|
|
||||||
let findi f seq =
|
let find = find_map
|
||||||
|
|
||||||
|
let find_mapi f seq =
|
||||||
let i = ref 0 in
|
let i = ref 0 in
|
||||||
let r = ref None in
|
let r = ref None in
|
||||||
begin
|
begin
|
||||||
|
|
@ -724,7 +726,9 @@ let findi f seq =
|
||||||
end;
|
end;
|
||||||
!r
|
!r
|
||||||
|
|
||||||
let find_pred f seq = find (fun x -> if f x then Some x else None) seq
|
let findi = find_mapi
|
||||||
|
|
||||||
|
let find_pred f seq = find_map (fun x -> if f x then Some x else None) seq
|
||||||
|
|
||||||
let find_pred_exn f seq = match find_pred f seq with
|
let find_pred_exn f seq = match find_pred f seq with
|
||||||
| Some x -> x
|
| Some x -> x
|
||||||
|
|
|
||||||
|
|
@ -144,10 +144,18 @@ val find : ('a -> 'b option) -> 'a t -> 'b option
|
||||||
(** Find the first element on which the function doesn't return [None]
|
(** Find the first element on which the function doesn't return [None]
|
||||||
@since 0.5 *)
|
@since 0.5 *)
|
||||||
|
|
||||||
|
val find_map : ('a -> 'b option) -> 'a t -> 'b option
|
||||||
|
(** Alias to {!find}
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val findi : (int -> 'a -> 'b option) -> 'a t -> 'b option
|
val findi : (int -> 'a -> 'b option) -> 'a t -> 'b option
|
||||||
(** Indexed version of {!find}
|
(** Indexed version of {!find}
|
||||||
@since 0.9 *)
|
@since 0.9 *)
|
||||||
|
|
||||||
|
val find_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b option
|
||||||
|
(** Alias to {!findi}
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
val find_pred : ('a -> bool) -> 'a t -> 'a option
|
val find_pred : ('a -> bool) -> 'a t -> 'a option
|
||||||
(** [find_pred p l] finds the first element of [l] that satisfies [p],
|
(** [find_pred p l] finds the first element of [l] that satisfies [p],
|
||||||
or returns [None] if no element satisfies [p]
|
or returns [None] if no element satisfies [p]
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue