package batteries
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=ea26b5c72e6731e59d856626049cca4d
sha512=55975b62c26f6db77433a3ac31f97af609fc6789bb62ac38b267249c78fd44ff37fe81901f1cf560857b9493a6046dd37b0d1c0234c66bd59e52843aac3ce6cb
doc/batteries.unthreaded/BatResult/index.html
Module BatResultSource
Monadic results of computations that can raise exceptions
The type of a result. A result is either Ok x carrying the normal return value x or is Error e carrying some indication of an error. The value associated with a bad result is usually an exception (exn) that can be raised.
value r ~default is v if r is Ok v and default otherwise.
default d r evaluates to d if r is Error else x when r is Ok x.
get (Ok x) returns x, and get (Error e) raises e. This function is, in a way, the opposite of the catch function
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.
As catch but two parameters. This saves a closure construction
As catch but three parameters. This saves a closure construction
bind r f is f v if r is Ok v and r if r is Error _.
join rr is r if rr is Ok r and rr if rr is Error _.
map f r is Ok (f v) if r is Ok v and r if r is Error _.
map_error f r is Error (f e) if r is Error e and r if r is Ok _.
map_both f g (Ok x) returns Ok (f x) and map_both f g (Error e) returns Error (g e).
map_default d f r evaluates to d if r is Error else f x when r is Ok x
fold ~ok ~error r is ok v if r is Ok v and error e if r is Error e.
iter_error f r is f e if r is Error e and () otherwise.
Predicates and comparisons
val equal :
ok:('a -> 'a -> bool) ->
error:('e -> 'e -> bool) ->
('a, 'e) t ->
('a, 'e) t ->
boolequal ~ok ~error r0 r1 tests equality of r0 and r1 using ok and error to respectively compare values wrapped by Ok _ and Error _.
val compare :
ok:('a -> 'a -> int) ->
error:('e -> 'e -> int) ->
('a, 'e) t ->
('a, 'e) t ->
intcompare ~ok ~error r0 r1 totally orders r0 and r1 using ok and error to respectively compare values wrapped by Ok _ and Error _. Ok _ values are smaller than Error _ values.
Converting
to_option r is r as an option, mapping Ok v to Some v and Error _ to None.
to_seq r is r as a sequence. Ok v is the singleton sequence containing v and Error _ is the empty sequence.
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.
Infix
val print :
('b BatInnerIO.output -> 'a -> unit) ->
'b BatInnerIO.output ->
('a, exn) t ->
unitPrint a result as Ok(x) or Error(exn)