Utils: add first_some

This commit is contained in:
Vincent Bernardoff 2016-11-29 14:51:36 +01:00
parent 62a1be15b7
commit ec922f6821
2 changed files with 6 additions and 0 deletions

View File

@ -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

View File

@ -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