CCResultError Monad
Uses the new "result" type from OCaml 4.03.
type 'a printer = Stdlib.Format.formatter -> 'a -> unitThe type for result values. Either a value Ok v or an error Error e.
val ok : 'a -> ('a, 'e) resultok v is Ok v.
val error : 'e -> ('a, 'e) resulterror e is Error e.
val value : ('a, 'e) result -> default:'a -> 'avalue r ~default is v if r is Ok v and default otherwise.
val get_ok : ('a, 'e) result -> 'aget_ok r is v if r is Ok v and raise otherwise.
val get_error : ('a, 'e) result -> 'eget_error r is e if r is Error e and raise otherwise.
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 _.
val fold : ok:('a -> 'c) -> error:('e -> 'c) -> ('a, 'e) result -> 'cfold ~ok ~error r is ok v if r is Ok v and error e if r is Error e.
val iter : ('a -> unit) -> ('a, 'e) result -> unititer f r is f v if r is Ok v and () otherwise.
val iter_error : ('e -> unit) -> ('a, 'e) result -> unititer_error f r is f e if r is Error e and () otherwise.
val is_ok : ('a, 'e) result -> boolis_ok r is true if and only if r is Ok _.
val is_error : ('a, 'e) result -> boolis_error r is true if and only if r is Error _.
val to_option : ('a, 'e) result -> 'a optionto_option r is r as an option, mapping Ok v to Some v and Error _ to None.
val to_list : ('a, 'e) result -> 'a listto_list r is [v] if r is Ok v and [] otherwise.
val to_seq : ('a, 'e) result -> 'a Stdlib.Seq.tto_seq r is r as a sequence. Ok v is the singleton sequence containing v and Error _ is the empty sequence.
val return : 'a -> ('a, 'err) tSuccessfully return a value.
val fail : 'err -> ('a, 'err) tFail with an error.
val of_exn : exn -> ('a, string) tof_exn e uses Printexc to print the exception as a string.
val of_exn_trace : exn -> ('a, string) tof_exn_trace e is similar to of_exn e, but it adds the stacktrace to the error message.
Remember to call Printexc.record_backtrace true and compile with the debug flag for this to work.
val fail_printf : ('a, Stdlib.Buffer.t, unit, ('b, string) t) format4 -> 'afail_printf format uses format to obtain an error message and then returns Error msg.
val fail_fprintf :
('a, Stdlib.Format.formatter, unit, ('b, string) t) format4 ->
'afail_fprintf format uses format to obtain an error message and then returns Error msg.
add_ctx msg leaves Ok x untouched, but transforms Error s into Error s' where s' contains the additional context given by msg.
val add_ctxf :
('a, Stdlib.Format.formatter, unit, ('b, string) t -> ('b, string) t) format4 ->
'aadd_ctxf format_message is similar to add_ctx but with Format for printing the message (eagerly). Example:
add_ctxf "message(number %d, foo: %B)" 42 true (Error "error)"Map a fallible operation through an option.
Like map, but also with a function that can transform the error message in case of failure.
val iter_err : ('err -> unit) -> (_, 'err) t -> unitAlias of iter_error
val get_exn : ('a, _) t -> 'aExtract the value x from Ok x, fails otherwise. You should be careful with this function, and favor other combinators whenever possible.
val get_or : ('a, _) t -> default:'a -> 'aget_or e ~default returns x if e = Ok x, default otherwise.
val apply_or : ('a -> ('a, _) t) -> 'a -> 'aapply_or f x returns the original x if f fails, or unwraps f x if it succeeds. Useful for piping preprocessing functions together (such as string processing), turning functions like "remove" into "remove_if_it_exists".
val get_or_failwith : ('a, string) t -> 'aget_or_failwith e returns x if e = Ok x, fails otherwise.
val get_lazy : ('b -> 'a) -> ('a, 'b) t -> 'aget_lazy default_fn x unwraps x, but if x = Error e it returns default_fr e instead.
val map_or : ('a -> 'b) -> ('a, 'c) t -> default:'b -> 'bmap_or f e ~default returns f x if e = Ok x, default otherwise.
val catch : ('a, 'err) t -> ok:('a -> 'b) -> err:('err -> 'b) -> 'bcatch e ~ok ~err calls either ok or err depending on the value of e.
val retract : ('a, 'a) t -> 'aretract r collapse r to v if r is either Ok v or Error v.
Kleisli composition. Monadic equivalent of CCFun.compose.
val fold_ok : ('a -> 'b -> 'a) -> 'a -> ('b, _) t -> 'afold_ok f acc r will compute f acc x if r=Ok x, and return acc otherwise, as if the result were a mere option.
val guard : (unit -> 'a) -> ('a, exn) tguard f runs f () and returns its result wrapped in Ok. If f () raises some exception e, then it fails with Error e.
val guard_str_trace : (unit -> 'a) -> ('a, string) tLike guard_str but uses of_exn_trace instead of of_exn so that the stack trace is printed.
val wrap2 : ('a -> 'b -> 'c) -> 'a -> 'b -> ('c, exn) tLike guard but gives the function two arguments.
val wrap3 : ('a -> 'b -> 'c -> 'd) -> 'a -> 'b -> 'c -> ('d, exn) tLike guard but gives the function three arguments.
product a b, in case of success, returns Ok (o, o') with the ok values of a and b. Otherwise, it fails, and the error of a is chosen over the error of b if both fail.
module Infix : sig ... endinclude module type of InfixMonadic composition. e >>= f proceeds as f x if e is Ok x or returns e if e is an Error.
a <*> b evaluates a and b, and, in case of success, returns Ok (a b). Otherwise, it fails, and the error of a is chosen over the error of b if both fail.
Same as map_l id: returns Ok [x1;…;xn] if l=[Ok x1; …; Ok xn], or the first error otherwise.
map_l f [a1; …; an] applies the function f to a1, …, an ,and, in case of success for every element, returns the list of Ok-value. Otherwise, it fails and returns the first error encountered. Tail-recursive.
choose l selects a member of l that is a Ok _ value, or returns Error l otherwise, where l is the list of errors.
retry n f calls f at most n times, returning the first result of f () that doesn't fail. If f fails n times, retry n f fails with the list of successive errors.
module type MONAD = sig ... endval of_opt : 'a option -> ('a, string) tof_opt opt converts Some v to Ok v and None to Error "of_opt".