Permalink
Browse files

Update Result with many more functions and a bit better documentation

  • Loading branch information...
1 parent 975a7e9 commit 8831b0ae6e50eebd3a38d82a91baa066d4c7d151 @thelema thelema committed Dec 12, 2011
Showing with 92 additions and 35 deletions.
  1. +23 −10 src/batResult.ml
  2. +69 −25 src/batResult.mli
View
@@ -16,23 +16,36 @@ let to_option = function
| Ok x -> Some x
| Bad _-> None
-let bind m k = match m with
- | Ok x -> k x
- | Bad _ as e -> e
-
-let (>>=) = bind
-
-let return x = Ok x
-
let default def = function
| Ok x -> x
| Bad _ -> def
-let default_map def f = function
+let map_default def f = function
| Ok x -> f x
| Bad _ -> def
+let is_ok = function Ok _ -> true | Bad _ -> false
+
+let is_bad = function Bad _ -> true | Ok _ -> false
+
+let get = function Ok x -> x | Bad e -> raise e
+
+let print print_val oc = function
+ | Ok x -> BatInnerIO.fprintf oc "Ok(%a)" print_val x
+ | Bad e -> BatInnerIO.fprintf oc "Bad(%a)" BatPrintexc.print e
+
+
+module Monad = struct
+ let bind m k = match m with
+ | Ok x -> k x
+ | Bad _ as e -> e
+
+ let return x = Ok x
+
+ let (>>=) = bind
+end
+
module Infix = struct
- let (>>=) = (>>=)
+ let (>>=) = Monad.bind
end
View
@@ -1,41 +1,85 @@
-open BatPervasives
(** Monadic results of computations that can raise exceptions *)
-type ('a, 'b) t = ('a, 'b) result
-(** The type of a result*)
+(** The type of a result. A result is either [Ok x] carrying the
+ normal return value [x] or is [Bad e] carrying some indication of an
+ error. The value associated with a bad result is usually an exception
+ ([exn]) that can be raised.
+ @since 1.0
+*)
+type ('a, 'b) t = ('a, 'b) BatPervasives.result = Ok of 'a | Bad of 'b
-val catch: ('a -> 'b) -> 'a -> ('b, exn) result
-(** Execute a function and catch any exception as a [!result]*)
+(** Execute a function and catch any exception as a result. This
+ function encapsulates code that could throw an exception and returns
+ that exception as a value.
+ @since 1.0
+*)
+val catch: ('a -> 'b) -> 'a -> ('b, exn) t
-val of_option: 'a option -> ('a, unit) result
-(** Convert an [option] to a [result]*)
+(** [get (Ok x)] returns [x], and [get (Bad e)] raises [e]. This
+ function is, in a way, the opposite of the [catch] function
+ @since 2.0
+*)
+val get : ('a, exn) t -> 'a
-val to_option: ('a, _) result -> 'a option
-(** Convert a [result] to an [option]*)
+(** [result d r] evaluates to [d] if [r] is [Bad] else [x] when [r] is
+ [Ok x]
+ @since 2.0
+*)
+val default: 'a -> ('a, _) t -> 'a
-val bind: ('a, 'b) result -> ('a -> ('c, 'b) result) -> ('c, 'b) result
-(** Monadic composition.
+(** [map_default d f r] evaluates to [d] if [r] is [Bad] else [f x]
+ when [r] is [Ok x]
+ @since 2.0
+*)
+val map_default : 'b -> ('a -> 'b) -> ('a, _) t -> 'b
- [bind r f] proceeds as [f x] if [r] is [Ok x], or returns [r] if
- [r] is an error.*)
+(** [is_ok (Ok _)] is [true], otherwise [false].
+ @since 2.0
+*)
+val is_ok : ('a, 'b) t -> bool
-val ( >>= ): ('a, 'b) result -> ('a -> ('c, 'b) result) -> ('c, 'b) result
-(** as [bind] *)
+(** [is_bad (Bad _)] is [false], otherwise [true]
+ @since 2.0
+*)
+val is_bad : ('a, 'b) t -> bool
-val return : 'a -> ('a, _) result
-(** Monadic return, just encapsulates the given value with Ok *)
+(** Convert an [option] to a [result]
+ @since 1.0 *)
+val of_option: 'a option -> ('a, unit) t
-val default: 'a -> ('a, _) result -> 'a
-(** [result d r] evaluates to [d] if [r] is [Bad] else [x] when [r] is
- [Ok x] *)
+(** Convert a [result] to an [option]
+ @since 1.0 *)
+val to_option: ('a, _) t -> 'a option
+
+(** {6 The Result Monad} *)
+
+(** This monad is very similar to the option monad, but instead of
+ being [None] when an error occurs, the first error in the sequence is
+ preserved as the return value. *)
+
+module Monad : sig
-val default_map : 'b -> ('a -> 'b) -> ('a, _) result -> 'b
-(** [default_map d f r] evaluates to [d] if [r] is [Bad] else [f x]
- when [r] is [Ok x] *)
+ (** Monadic composition.
+
+ [bind r f] proceeds as [f x] if [r] is [Ok x], or returns [r] if
+ [r] is an error.
+ @since 2.0
+*)
+ val bind: ('a, 'b) t -> ('a -> ('c, 'b) t) -> ('c, 'b) t
+
+ (** as [bind] *)
+ val ( >>= ): ('a, 'b) t -> ('a -> ('c, 'b) t) -> ('c, 'b) t
+
+ (** Monadic return, just encapsulates the given value with Ok *)
+ val return : 'a -> ('a, _) t
+end
+(** {6 Infix} *)
-(** {6 Infix submodule regrouping all infix operators} *)
+(** This infix module provides the operator [(>>=)] *)
module Infix : sig
- val ( >>= ): ('a, 'b) result -> ('a -> ('c, 'b) result) -> ('c, 'b) result
+ val ( >>= ): ('a, 'b) t -> ('a -> ('c, 'b) t) -> ('c, 'b) t
end
+(** Print a result as Ok(x) or Bad(exn) *)
+val print : ('b BatInnerIO.output -> 'a -> unit) -> 'b BatInnerIO.output -> ('a, exn) t -> unit

0 comments on commit 8831b0a

Please sign in to comment.