From 4f8370e5248ccabb1fa1e28bab749070b543bd3f Mon Sep 17 00:00:00 2001 From: Sam Blackshear Date: Mon, 12 Sep 2016 15:36:21 -0700 Subject: [PATCH] [utils] add find_mapi_opt utility Reviewed By: jeremydubreil Differential Revision: D3851678 fbshipit-source-id: dcf1c6d --- infer/src/backend/iList.ml | 11 +++++++++++ infer/src/backend/iList.mli | 3 +++ 2 files changed, 14 insertions(+) diff --git a/infer/src/backend/iList.ml b/infer/src/backend/iList.ml index 1a534b3af..dccaf73e8 100644 --- a/infer/src/backend/iList.ml +++ b/infer/src/backend/iList.ml @@ -221,6 +221,17 @@ let rec find_map_opt f = function then e' else find_map_opt f l' +(** Like find_map_opt, but with indices *) +let find_mapi_opt (f : int -> 'a -> 'b option) l = + let rec find_mapi_opt_ f i = function + | [] -> None + | e :: l' -> + let e' = f i e in + if e' <> None + then e' + else find_mapi_opt_ f (i + 1) l' in + find_mapi_opt_ f 0 l + let to_string f l = let rec aux l = match l with diff --git a/infer/src/backend/iList.mli b/infer/src/backend/iList.mli index 850f1551e..a7d1c5edf 100644 --- a/infer/src/backend/iList.mli +++ b/infer/src/backend/iList.mli @@ -112,4 +112,7 @@ val map2 : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list (** Return the first non-None result found when applying f to elements of l *) val find_map_opt : ('a -> 'b option) -> 'a list -> 'b option +(** Like find_map_opt, but with indices *) +val find_mapi_opt : (int -> 'a -> 'b option) -> 'a list -> 'b option + val to_string : ('a -> string) -> 'a list -> string