From ec922f682120f745e445e8dc2449f2e3459d582e Mon Sep 17 00:00:00 2001 From: Vincent Bernardoff Date: Tue, 29 Nov 2016 14:51:36 +0100 Subject: [PATCH] Utils: add first_some --- src/utils/utils.ml | 5 +++++ src/utils/utils.mli | 1 + 2 files changed, 6 insertions(+) diff --git a/src/utils/utils.ml b/src/utils/utils.ml index 1f1a45717..b52aab3c9 100644 --- a/src/utils/utils.ml +++ b/src/utils/utils.ml @@ -112,6 +112,11 @@ let unopt_list l = let may_cons xs x = match x with None -> xs | Some x -> x :: xs in List.rev @@ List.fold_left may_cons [] l +let first_some a b = match a, b with + | None, None -> None + | None, Some v -> Some v + | Some v, _ -> Some v + let filter_map f l = let may_cons xs x = match f x with None -> xs | Some x -> x :: xs in List.rev @@ List.fold_left may_cons [] l diff --git a/src/utils/utils.mli b/src/utils/utils.mli index 4ca6ea823..e42aaa302 100644 --- a/src/utils/utils.mli +++ b/src/utils/utils.mli @@ -31,6 +31,7 @@ val apply_option: f:('a -> 'b option) -> 'a option -> 'b option val iter_option: f:('a -> unit) -> 'a option -> unit val unopt: 'a -> 'a option -> 'a val unopt_list: 'a option list -> 'a list +val first_some: 'a option -> 'a option -> 'a option val display_paragraph: Format.formatter -> string -> unit