Prelude.ResultResult (error) monad.
See Rresult for a much more sophisticated package.
(bind r f) is (Ok (f v)) if (r = Ok v) and r if (r = Error e).
(>>=) is bind.
(map f r) is (bind r (fun v -> Ok (f v))).
(>>|) is map.
val (>=>) :
('a -> ('b, 'c) Stdlib.result) ->
('b -> ('d, 'c) Stdlib.result) ->
'a ->
('d, 'c) Stdlib.result(>=>) is monadic composition: ((f >=> g) x = (f x) >>= g)
(join r) is v if r = Ok v and r otherwise.
This is particularly useful when nesting trap's:
trap Exn.to_string (withcd (fun _ -> trap Exn.to_string readfile "NONEXISTENT")) "/etc"
= Ok (Error "NONEXISTENT: No such file or directory") whereas:
trap Exn.to_string (withcd (fun _ -> trap Exn.to_string readfile "NONEXISTENT")) "/etc" |> join
= Error "NONEXISTENT: No such file or directory" (reduce rs) is the list of all the values carried by Ok's in the list, with all the Error's tossed out.
(on_error r f) is (f e) if r is (Error e) and is otherwise (Ok x).
(>>/) is on_error
a or b is Ok x if (a = Ok x) and is otherwise b.
( //* ) is always.
(trap e f x) is (Ok (f x)) unless an exception exn is raised, in which case it is (Error (e exn)).
(trapc c f x) is (Ok (f x)) unless an exception exn is raised, in which case it is (Error c).
(witherr f r) is (Error (f e)) iff (r = Error e) and is otherwise r.
(witherrc e r) is (Error e) iff (r = Error _) and is otherwise r.
(to_bool result) is true iff result is Ok _ and false otherwise.
(of_bool ?err x bool) converts true to Ok x and false to Error err, where the default value for err is x.
(to_option r) is (Some x) iff (r = Ok x) and is None otherwise.
(of_option err o) is (Ok x) iff (o = Some x) and is (Error err) otherwise.
(some_error r) is in some sense the inverse of to_option: it is None if r is Ok, and (Some err) if r is (Error err).
module Seq : sig ... endFunctions on sequences of results.
(to_string ~error ~ok r) is a string representation of r, where ~error converts the Error case and ~ok the Ok case.
(print ~error ~ok) = (to_string ~error ~ok >> print_endline).
(random ~error ~ok ()) is either (Ok (ok ())) or (Error (error ())) with equal probability.
module Ops : sig ... endInfix operators.