123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2020-2021 Nomadic Labs <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)(** A replacement for {!Stdlib.Option} which
- is exception-safe,
- includes Lwt-, result-, and Lwt-result-aware traversors.
See {!Lwtreslib} and {!Seq} for general description of traversors and the
meaning of [_s], [_e], and [_es] suffixes. *)moduletypeS=sigtype'at='aoption=None|Someof'avalnone:'aoptionvalnone_e:('aoption,'trace)resultvalnone_s:'aoptionLwt.tvalnone_es:('aoption,'trace)resultLwt.tvalsome:'a->'aoptionvalsome_unit:unitoptionvalsome_unit_e:(unitoption,'error)resultvalsome_unit_s:unitoptionLwt.tvalsome_unit_es:(unitoption,'error)resultLwt.tvalsome_nil:'alistoptionvalsome_nil_e:('alistoption,'error)resultvalsome_nil_s:'alistoptionLwt.tvalsome_nil_es:('alistoption,'error)resultLwt.tvalsome_true:booloptionvalsome_true_e:(booloption,'error)resultvalsome_true_s:booloptionLwt.tvalsome_true_es:(booloption,'error)resultLwt.tvalsome_false:booloptionvalsome_false_e:(booloption,'error)resultvalsome_false_s:booloptionLwt.tvalsome_false_es:(booloption,'error)resultLwt.tvalsome_e:'a->('aoption,'trace)resultvalsome_s:'a->'aoptionLwt.tvalsome_es:'a->('aoption,'trace)resultLwt.tvalvalue:'aoption->default:'a->'avalvalue_e:'aoption->error:'trace->('a,'trace)resultvalvalue_f:'aoption->default:(unit->'a)->'avalvalue_fe:'aoption->error:(unit->'trace)->('a,'trace)resultvalbind:'aoption->('a->'boption)->'boptionvaljoin:'aoptionoption->'aoption(** [either] picks the first [Some _] value of its arguments if any.
More formally, [either (Some x) _] is [Some x], [either None (Some y)] is
[Some y], and [either None None] is [None]. *)valeither:'aoption->'aoption->'aoptionvaleither_f:'aoption->(unit->'aoption)->'aoptionvalmerge:('a->'a->'a)->'aoption->'aoption->'aoptionvalmerge_e:('a->'a->('a,'e)result)->'aoption->'aoption->('aoption,'e)resultvalmerge_s:('a->'a->'aLwt.t)->'aoption->'aoption->'aoptionLwt.tvalmerge_es:('a->'a->('a,'e)resultLwt.t)->'aoption->'aoption->('aoption,'e)resultLwt.tvalmap:('a->'b)->'aoption->'boptionvalmap_s:('a->'bLwt.t)->'aoption->'boptionLwt.tvalmap_e:('a->('b,'trace)result)->'aoption->('boption,'trace)resultvalmap_es:('a->('b,'trace)resultLwt.t)->'aoption->('boption,'trace)resultLwt.tvalfold:none:'a->some:('b->'a)->'boption->'avalfold_s:none:'a->some:('b->'aLwt.t)->'boption->'aLwt.tvalfold_f:none:(unit->'a)->some:('b->'a)->'boption->'a(** [filter p o] is [Some x] iff [o] is [Some x] and [p o] is [true].
In other words, [filter] is like [List.filter] if [option] is the type of
lists of either zero or one elements. In fact, the following equality
holds for all [p] and for all [o]:
[Option.filter p o = List.hd (List.filter p (Option.to_list o))]
The other [filter] variants below are also equivalent to their [List]
counterpart and a similar equality holds. *)valfilter:('a->bool)->'aoption->'aoption(** [filter_map] is the [Option] counterpart to [List]'s [filter_map].
Incidentally, [filter_map f o] is also [bind o f]. *)valfilter_map:('a->'boption)->'aoption->'boption(** [filter_s] is [filter] where the predicate returns a promise. *)valfilter_s:('a->boolLwt.t)->'aoption->'aoptionLwt.t(** [filter_map_s] is [filter_map] where the function returns a promise. *)valfilter_map_s:('a->'boptionLwt.t)->'aoption->'boptionLwt.t(** [filter_e] is [filter] where the predicate returns a [result]. *)valfilter_e:('a->(bool,'e)result)->'aoption->('aoption,'e)result(** [filter_map_e] is [filter_map] where the function returns a [result]. *)valfilter_map_e:('a->('boption,'e)result)->'aoption->('boption,'e)result(** [filter_es] is [filter] where the predicate returns a promise of a [result]. *)valfilter_es:('a->(bool,'e)resultLwt.t)->'aoption->('aoption,'e)resultLwt.t(** [filter_map_es] is [filter_map] where the function returns a promise of a [result]. *)valfilter_map_es:('a->('boption,'e)resultLwt.t)->'aoption->('boption,'e)resultLwt.t(** [filter_ok o] is [Some x] iff [o] is [Some (Ok x)]. *)valfilter_ok:('a,'e)resultoption->'aoption(** [filter_error o] is [Some x] iff [o] is [Some (Error x)]. *)valfilter_error:('a,'e)resultoption->'eoption(** [filter_left o] is [Some x] iff [o] is [Some (Either.Left x)]. *)valfilter_left:('a,'b)Either.toption->'aoption(** [filter_right o] is [Some x] iff [o] is [Some (Either.Right x)]. *)valfilter_right:('a,'b)Either.toption->'boptionvaliter:('a->unit)->'aoption->unitvaliter_s:('a->unitLwt.t)->'aoption->unitLwt.tvaliter_e:('a->(unit,'trace)result)->'aoption->(unit,'trace)resultvaliter_es:('a->(unit,'trace)resultLwt.t)->'aoption->(unit,'trace)resultLwt.tvalis_none:'aoption->boolvalis_some:'aoption->boolvalequal:('a->'a->bool)->'aoption->'aoption->boolvalcompare:('a->'a->int)->'aoption->'aoption->intvalto_result:none:'trace->'aoption->('a,'trace)resultvalof_result:('a,'e)result->'aoptionvalto_list:'aoption->'alistvalto_seq:'aoption->'aStdlib.Seq.t(** [catch f] is [Some (f ())] if [f] does not raise an exception, it is
[None] otherwise.
You should only use [catch] when you truly do not care about
what exception may be raised during the evaluation of [f ()]. If you need
to inspect the raised exception, or if you need to pass it along, consider
{!Result.catch} instead.
If [catch_only] is set, then only exceptions [e] such that [catch_only e]
is [true] are caught.
Whether [catch_only] is set or not, this function never catches
non-deterministic runtime exceptions of OCaml such as {!Stack_overflow}
and {!Out_of_memory}. *)valcatch:?catch_only:(exn->bool)->(unit->'a)->'aoption(** [catch_o f] is equivalent to [join @@ catch f]. In other words, it is
[f ()] if [f] doesn't raise any exception, and it is [None] otherwise.
[catch_only] has the same behaviour and limitations as with [catch]. *)valcatch_o:?catch_only:(exn->bool)->(unit->'aoption)->'aoption(** [catch_s f] is a promise that resolves to [Some x] if and when [f ()]
resolves to [x]. Alternatively, it resolves to [None] if and when [f ()]
is rejected.
You should only use [catch_s] when you truly do not care about
what exception may be raised during the evaluation of [f ()]. If you need
to inspect the raised exception, or if you need to pass it along, consider
{!Result.catch_s} instead.
If [catch_only] is set, then only exceptions [e] such that [catch_only e]
is [true] are caught.
Whether [catch_only] is set or not, this function never catches
non-deterministic runtime exceptions of OCaml such as {!Stack_overflow}
and {!Out_of_memory}. *)valcatch_s:?catch_only:(exn->bool)->(unit->'aLwt.t)->'aoptionLwt.t(** [catch_os f] is like [catch_s f] where [f] returns a promise that resolves
to an option. [catch_os f] resolves to [None] if [f ()] resolves to
[None] or is rejected. It resolves to [Some _] if [f ()] does.
[catch_only] has the same behaviour and limitations as with [catch]. *)valcatch_os:?catch_only:(exn->bool)->(unit->'aoptionLwt.t)->'aoptionLwt.tend