From 7e28feadd6394483b6f527d5aed7d663e189596e Mon Sep 17 00:00:00 2001 From: Hugo Herbelin Date: Tue, 12 Jul 2016 23:26:44 +0200 Subject: Upgrading some local function as a general-purpose combinator Option.List.map. --- lib/option.ml | 10 ++++++++++ lib/option.mli | 6 ++++++ 2 files changed, 16 insertions(+) (limited to 'lib') diff --git a/lib/option.ml b/lib/option.ml index fbb883d30a..f3047a3ca7 100644 --- a/lib/option.ml +++ b/lib/option.ml @@ -188,4 +188,14 @@ module List = |None -> find f t |x -> x + let map f l = + let rec aux f l = match l with + | [] -> [] + | x :: l -> + match f x with + | None -> raise Exit + | Some y -> y :: aux f l + in + try Some (aux f l) with Exit -> None + end diff --git a/lib/option.mli b/lib/option.mli index 5e085620e7..a336c895cc 100644 --- a/lib/option.mli +++ b/lib/option.mli @@ -123,4 +123,10 @@ module List : sig val flatten : 'a option list -> 'a list val find : ('a -> 'b option) -> 'a list -> 'b option + + (** [List.map f [a1;...;an]] is the list [Some [b1;...;bn]] if + for all i, there is a [bi] such that [f ai] is [Some bi]; it is + [None] if, for at least one i, [f ai] is [None]. *) + val map : ('a -> 'b option) -> 'a list -> 'b list option + end -- cgit v1.2.3