From 4b704d9d1a0c14fa087448294e2707cb1c1a98af Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sun, 24 May 2015 21:19:01 +0200 Subject: [PATCH] rename `CCList.find -> CClist.find_map` (same for `findi`), deprecate old name --- src/core/CCList.ml | 9 ++++++--- src/core/CCList.mli | 14 +++++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/core/CCList.ml b/src/core/CCList.ml index 57b5189a..c43904cc 100644 --- a/src/core/CCList.ml +++ b/src/core/CCList.ml @@ -401,7 +401,7 @@ let find_pred_exn p l = match find_pred p l with find_pred (fun x -> x < 10) (1 -- 9) = Some 1 *) -let findi f l = +let find_mapi f l = let rec aux f i = function | [] -> None | x::l' -> @@ -410,9 +410,12 @@ let findi f l = | None -> aux f (i+1) l' in aux f 0 l -let find f l = findi (fun _ -> f) l +let find_map f l = find_mapi (fun _ -> f) l -let find_idx p l = findi (fun i x -> if p x then Some (i, x) else None) l +let find = find_map +let findi = find_mapi + +let find_idx p l = find_mapi (fun i x -> if p x then Some (i, x) else None) l (*$T find (fun x -> if x=3 then Some "a" else None) [1;2;3;4] = Some "a" diff --git a/src/core/CCList.mli b/src/core/CCList.mli index 4bc71c5e..bc4531be 100644 --- a/src/core/CCList.mli +++ b/src/core/CCList.mli @@ -119,13 +119,21 @@ val find_pred_exn : ('a -> bool) -> 'a t -> 'a @raise Not_found if no such element is found @since NEXT_RELEASE *) -val find : ('a -> 'b option) -> 'a t -> 'b option +val find_map : ('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] *) + the call returns [None] + @since NEXT_RELEASE *) + +val find : ('a -> 'b option) -> 'a list -> 'b option +(** @deprecated in favor of {!find_map}, for the name is too confusing *) + +val find_mapi : (int -> 'a -> 'b option) -> 'a t -> 'b option +(** Like {!find_map}, but also pass the index to the predicate function. + @since NEXT_RELEASE *) val findi : (int -> 'a -> 'b option) -> 'a t -> 'b option -(** Like {!find}, but also pass the index to the predicate function. +(** @deprecated in favor of {!find_mapi}, name is too confusing @since 0.3.4 *) val find_idx : ('a -> bool) -> 'a t -> (int * 'a) option