add find_map{,i} as better alias to existing functions

This commit is contained in:
Simon Cruanes 2017-02-02 21:32:00 +01:00
parent ffe157a1c6
commit a41a997c8f
2 changed files with 15 additions and 3 deletions

View file

@ -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

View file

@ -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]