From 5d768aeeb20e6f941036edb4854766276f28aa8a Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sat, 20 May 2017 13:18:36 +0200 Subject: [PATCH] add `CCArray.find_map{,_i}`, deprecated older names (closes #129) --- src/core/CCArray.ml | 10 ++++++---- src/core/CCArray.mli | 19 +++++++++++++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/core/CCArray.ml b/src/core/CCArray.ml index 44d93b16..76ed250a 100644 --- a/src/core/CCArray.ml +++ b/src/core/CCArray.ml @@ -212,11 +212,13 @@ let rec find_aux f a i = | Some _ as res -> res | None -> find_aux f a (i+1) -let find f a = - find_aux (fun _ -> f ) a 0 +let find_map f a = find_aux (fun _ -> f ) a 0 -let findi f a = - find_aux f a 0 +let find = find_map + +let find_map_i f a = find_aux f a 0 + +let findi = find_map_i let find_idx p a = find_aux (fun i x -> if p x then Some (i,x) else None) a 0 diff --git a/src/core/CCArray.mli b/src/core/CCArray.mli index e8d5c8e2..4ecaade7 100644 --- a/src/core/CCArray.mli +++ b/src/core/CCArray.mli @@ -88,13 +88,24 @@ val sort_ranking : ('a -> 'a -> int) -> 'a t -> int array [lookup_exn a.(i) (sorted a) = (sorted_ranking a).(i)] @since 1.0 *) +val find_map : ('a -> 'b option) -> 'a t -> 'b option +(** [find_map f a] returns [Some y] if there is an element [x] such + that [f x = Some y], else it returns [None] + @since NEXT_RELEASE +*) + val find : ('a -> 'b option) -> 'a t -> 'b option -(** [find f a] returns [Some y] if there is an element [x] such - that [f x = Some y], else it returns [None] *) +(** Alias to {!find_map} + @deprecated since NEXT_RELEASE *) + +val find_map_i : (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. - @since 0.3.4 *) +(** Alias to {!find_map_i} + @since 0.3.4 + @deprecated since NEXT_RELEASE *) val find_idx : ('a -> bool) -> 'a t -> (int * 'a) option (** [find_idx p x] returns [Some (i,x)] where [x] is the [i]-th element of [l],