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 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'lib/option.ml') 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 -- cgit v1.2.3 From 3f4c29f8258d627c7dbacbf1157825d96b146a6d Mon Sep 17 00:00:00 2001 From: Hugo Herbelin Date: Fri, 5 May 2017 17:34:43 +0200 Subject: Cosmetic: unifying style within option.ml. --- lib/option.ml | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) (limited to 'lib/option.ml') diff --git a/lib/option.ml b/lib/option.ml index f3047a3ca7..50fdd079dc 100644 --- a/lib/option.ml +++ b/lib/option.ml @@ -20,24 +20,24 @@ let has_some = function | _ -> true let is_empty = function -| None -> true -| Some _ -> false + | None -> true + | Some _ -> false (** Lifting equality onto option types. *) let equal f x y = match x, y with -| None, None -> true -| Some x, Some y -> f x y -| _, _ -> false + | None, None -> true + | Some x, Some y -> f x y + | _, _ -> false let compare f x y = match x, y with -| None, None -> 0 -| Some x, Some y -> f x y -| None, Some _ -> -1 -| Some _, None -> 1 + | None, None -> 0 + | Some x, Some y -> f x y + | None, Some _ -> -1 + | Some _, None -> 1 let hash f = function -| None -> 0 -| Some x -> f x + | None -> 0 + | Some x -> f x exception IsNone @@ -57,13 +57,11 @@ let init b x = else None - (** [flatten x] is [Some y] if [x] is [Some (Some y)] and [None] otherwise. *) let flatten = function | Some (Some y) -> Some y | _ -> None - (** [append x y] is the first element of the concatenation of [x] and [y] seen as lists. *) let append o1 o2 = @@ -134,6 +132,7 @@ let cata f a = function | Some c -> f c | None -> a + (** {6 More Specific operations} ***) (** [default a x] is [y] if [x] is [Some y] and [a] otherwise. *) @@ -165,7 +164,6 @@ let lift2 f x y = | _,_ -> None - (** {6 Operations with Lists} *) module List = @@ -183,10 +181,10 @@ module List = | [] -> [] let rec find f = function - |[] -> None - |h :: t -> match f h with - |None -> find f t - |x -> x + | [] -> None + | h :: t -> match f h with + | None -> find f t + | x -> x let map f l = let rec aux f l = match l with -- cgit v1.2.3