package containers
Install
Dune Dependency
Authors
Maintainers
Sources
md5=00ba20a8c8824991cab96aff8f8e5a3c
sha512=4126045d58b9408eecaaad005c334081ade151f291dafc32ce9a33e8f796d41f72df62b617528f272996f3c93cb9b4293545ab86ac416288960f5a2b60b41932
doc/containers/CCResult/index.html
Module CCResult
Source
Error Monad
Uses the new "result" type from OCaml 4.03.
Fast internal iterator.
Basics
of_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.
fail_printf format
uses format
to obtain an error message and then returns Error msg
.
fail_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
.
Like map
, but also with a function that can transform the error message in case of failure.
Extract the value x
from Ok x
, fails otherwise. You should be careful with this function, and favor other combinators whenever possible.
get_or e ~default
returns x
if e = Ok x
, default
otherwise.
get_or_failwith e
returns x
if e = Ok x
, fails otherwise.
get_lazy default_fn x
unwraps x
, but if x = Error e
it returns default_fr e
instead.
map_or f e ~default
returns f x
if e = Ok x
, default
otherwise.
catch e ~ok ~err
calls either ok
or err
depending on the value of e
.
fold ~ok ~error e
opens e
and, if e = Ok x
, returns ok x
, otherwise e = Error s
and it returns error s
.
fold_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.
Wrappers
guard f
runs f ()
and returns its result wrapped in Ok
. If f ()
raises some exception e
, then it fails with Error e
.
Like guard_str
but uses of_exn_trace
instead of of_exn
so that the stack trace is printed.
Like guard
but gives the function two arguments.
Like guard
but gives the function three arguments.
Applicative
join t
, in case of success, returns Ok o
from Ok (Ok o)
. Otherwise, it fails with Error e
where e
is the unwrapped error of t
.
both 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.
Infix
include module type of Infix
Infix version of map
with reversed arguments.
Monadic 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.
Let operators on OCaml >= 4.08.0, nothing otherwise
include CCShimsMkLet_.S2 with type ('a, 'e) t_let2 := ('a, 'e) result
Let operators on OCaml >= 4.08.0, nothing otherwise
include CCShimsMkLet_.S2 with type ('a, 'e) t_let2 := ('a, 'e) result
Collections
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.
Misc
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.