Add map_lazy, or_, or_lazy, to_result, to_result_lazy and of_result to CCOpt

This commit is contained in:
glennsl 2017-03-25 23:46:14 +01:00
parent 923e83b0fc
commit 6573a2dd4a
3 changed files with 47 additions and 3 deletions

View file

@ -18,3 +18,4 @@
- Roma Sokolov (@little-arhat) - Roma Sokolov (@little-arhat)
- Malcolm Matalka (`orbitz`) - Malcolm Matalka (`orbitz`)
- David Sheets (@dsheets) - David Sheets (@dsheets)
- Glenn Slotte (glennsl)

View file

@ -13,6 +13,10 @@ let map_or ~default f = function
| None -> default | None -> default
| Some x -> f x | Some x -> f x
let map_lazy default_fn f = function
| None -> default_fn ()
| Some x -> f x
let is_some = function let is_some = function
| None -> false | None -> false
| Some _ -> true | Some _ -> true
@ -54,10 +58,16 @@ let (<*>) f x = match f, x with
let (<$>) = map let (<$>) = map
let (<+>) a b = match a with let or_ ~else_ a = match a with
| None -> b | None -> else_
| Some _ -> a | 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 choice l = List.fold_left (<+>) None l
let map2 f o1 o2 = match o1, o2 with let map2 f o1 o2 = match o1, o2 with
@ -137,6 +147,18 @@ let of_list = function
| x::_ -> Some x | x::_ -> Some x
| [] -> None | [] -> 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 module Infix = struct
let (>|=) = (>|=) let (>|=) = (>|=)
let (>>=) = (>>=) let (>>=) = (>>=)

View file

@ -9,9 +9,13 @@ val map : ('a -> 'b) -> 'a t -> 'b t
(** Transform the element inside, if any *) (** Transform the element inside, if any *)
val map_or : default:'b -> ('a -> 'b) -> 'a t -> 'b 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 *) @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_some : _ t -> bool
val is_none : _ t -> bool val is_none : _ t -> bool
@ -94,6 +98,14 @@ val (<$>) : ('a -> 'b) -> 'a t -> 'b t
(** {2 Alternatives} *) (** {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 val (<+>) : 'a t -> 'a t -> 'a t
(** [a <+> b] is [a] if [a] is [Some _], [b] otherwise *) (** [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 val of_list : 'a list -> 'a t
(** Head of list, or [None] *) (** 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 sequence = ('a -> unit) -> unit
type 'a gen = unit -> 'a option type 'a gen = unit -> 'a option
type 'a printer = Format.formatter -> 'a -> unit type 'a printer = Format.formatter -> 'a -> unit