From 6573a2dd4a1c54eac6e6ba445f853035d946f89e Mon Sep 17 00:00:00 2001 From: glennsl Date: Sat, 25 Mar 2017 23:46:14 +0100 Subject: [PATCH] Add map_lazy, or_, or_lazy, to_result, to_result_lazy and of_result to CCOpt --- AUTHORS.adoc | 1 + src/core/CCOpt.ml | 26 ++++++++++++++++++++++++-- src/core/CCOpt.mli | 23 ++++++++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/AUTHORS.adoc b/AUTHORS.adoc index 007ed5ce..fd3da2dc 100644 --- a/AUTHORS.adoc +++ b/AUTHORS.adoc @@ -18,3 +18,4 @@ - Roma Sokolov (@little-arhat) - Malcolm Matalka (`orbitz`) - David Sheets (@dsheets) +- Glenn Slotte (glennsl) diff --git a/src/core/CCOpt.ml b/src/core/CCOpt.ml index 355473c2..396ce37e 100644 --- a/src/core/CCOpt.ml +++ b/src/core/CCOpt.ml @@ -13,6 +13,10 @@ let map_or ~default f = function | None -> default | Some x -> f x +let map_lazy default_fn f = function + | None -> default_fn () + | Some x -> f x + let is_some = function | None -> false | Some _ -> true @@ -54,10 +58,16 @@ let (<*>) f x = match f, x with let (<$>) = map -let (<+>) a b = match a with - | None -> b +let or_ ~else_ a = match a with + | None -> else_ | Some _ -> a +let or_lazy ~else_ a = match a with + | None -> else_ () + | Some _ -> a + +let (<+>) a b = or_ ~else_:b a + let choice l = List.fold_left (<+>) None l let map2 f o1 o2 = match o1, o2 with @@ -137,6 +147,18 @@ let of_list = function | x::_ -> Some x | [] -> None +let to_result err = function + | None -> Error err + | Some x -> Ok x + +let to_result_lazy err_fn = function + | None -> Error (err_fn ()) + | Some x -> Ok x + +let of_result = function + | Error _ -> None + | Ok x -> Some x + module Infix = struct let (>|=) = (>|=) let (>>=) = (>>=) diff --git a/src/core/CCOpt.mli b/src/core/CCOpt.mli index 1281fbe1..19c7eead 100644 --- a/src/core/CCOpt.mli +++ b/src/core/CCOpt.mli @@ -9,9 +9,13 @@ val map : ('a -> 'b) -> 'a t -> 'b t (** Transform the element inside, if any *) val map_or : default:'b -> ('a -> 'b) -> 'a t -> 'b -(** [map_or ~default f o] is [f x] if [o = Some x], [default otherwise] +(** [map_or ~default f o] is [f x] if [o = Some x], [default] otherwise @since 0.16 *) +val map_lazy : (unit -> 'b) -> ('a -> 'b) -> 'a t -> 'b +(** [map_lazy default_fn f o] if [f o] if [o = Some x], [default_fn ()] otherwise + @since NEXT_RELEASE *) + val is_some : _ t -> bool val is_none : _ t -> bool @@ -94,6 +98,14 @@ val (<$>) : ('a -> 'b) -> 'a t -> 'b t (** {2 Alternatives} *) +val or_ : else_:('a t) -> 'a t -> 'a t +(** [or_ ~else_ a] is [a] if [a] is [Some _], [else_] otherwise + @since NEXT_RELEASE *) + +val or_lazy : else_:(unit -> 'a t) -> 'a t -> 'a t +(** [or_lazy else_ a] is [a] if [a] is [Some _], [else_ ()] otherwise + @since NEXT_RELEASE *) + val (<+>) : 'a t -> 'a t -> 'a t (** [a <+> b] is [a] if [a] is [Some _], [b] otherwise *) @@ -118,6 +130,15 @@ val to_list : 'a t -> 'a list val of_list : 'a list -> 'a t (** Head of list, or [None] *) +val to_result : 'e -> 'a t -> ('a, 'e) Result.result +(** @since NEXT_RELEASE *) + +val to_result_lazy : (unit -> 'e) -> 'a t -> ('a, 'e) Result.result +(** @since NEXT_RELEASE *) + +val of_result : ('a, _) Result.result -> 'a t +(** @since NEXT_RELEASE *) + type 'a sequence = ('a -> unit) -> unit type 'a gen = unit -> 'a option type 'a printer = Format.formatter -> 'a -> unit