Source file seq.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
(** {1 Seq}
A replacement for {!Stdlib.Seq} which
- is exception-safe,
- includes Lwt-, result- and Lwt-result-aware traversal functions.
See {!Lwtreslib} for a general description of traversors and the meaning for
the name suffixes. A full description is also below.
Note the [Seq] module (along with the [Seq_*] modules) (unlike other
modules of Lwtreslib) uses submodules to organise different monadic
traversors. This is because the implementation of the [Seq] module is
delegated to the [Seqes] library which uses functors which produces
(sub)modules.
All traversal functions that are inside the [E] submodule are within the
result monad. Note that these functions have a "fail-early" behaviour: the
traversal is interrupted as soon as any of the intermediate application
fails (i.e., returns an [Error _]).
All traversal functions that are inside the [S] submodule are within the Lwt
monad. These functions traverse the elements sequentially: the promise for a
given step of the traversal is only initiated when the promise for the
previous step is resolved. Note that these functions have a fail-early
behaviour: the traversal is interrupted if any of the intermediate promise
is rejected.
All the traversal functions that are suffixed with [_p] are within Lwt.
These functions traverse the elements concurrently: the promise for all the
steps are created immediately. The suffix [_p] is chosen for similarity with
the {!Lwt_list} functions even though, as with {!Lwt_list}'s functions there
is no parallelism involved, only concurrency. Note that these functions have
a “best-effort” behaviour: the whole-traversal promise (i.e., the promise
returned by the [_p]-suffixed function) only resolves once each of the step
promises have resolved. Even if one of the step promise is rejected, the
whole-traversal promise is only rejected once all the other step promises
have resolved.
All the traversal functions that are inside the [ES] submodule are within
the combined error-and-Lwt monad. These function traverse the elements
sequentially with a fail-early behaviour for both rejection (as an Lwt
promise) and failure (as a result).
All the traversal functions that are suffixed with [_ep] are within the
combined error-and-Lwt monad. These function traverse the elements
concurrently with a best-effort behaviour.
*)
(** {2 Special consideration}
Because of the type of {!Stdlib.Seq.t}, some interactions with Lwt are not
possible. Specifically, note that the type includes the variant
[unit -> 'a node] which is not within Lwt nor within the result monad.
As a result, some of the traversals ([S.map], [E.map], etc.) cannot be
applied lazily.
Check-out the [S] variants ({!Seq_s.S}, {!Seq_e.S}, and
{!Seq_es.S}) that integrate the base sequence type better with the monads'
type. It is recommended that you use the variant as appropriate to your
traversal. Note the presence of [of_seq] in each of those variants to
convert from the standard [S.t]. *)
module type S = sig
(** {3 Common interface with Stdlib}
Note that some functions (namely [init], [take], and [drop]) are shadowed
with exception-less versions.
Note that [once] is not shadowed. Be careful when using [once]: the
resulting sequence is {e ephemeral} and using in a non-ephemeral way
raises an exception. As a safer alternative, you can use
[Seq_e.of_seq_once] which gives you a result-based (exception-less)
ephemeral sequence. *)
include
module type of Stdlib.Seq
with type 'a t = 'a Stdlib.Seq.t
and type 'a node = 'a Stdlib.Seq.node
(** {3 Lwtreslib-specific safety-shadowing} *)
val init :
when_negative_length:'err -> int -> (int -> 'a) -> ('a t, 'err) result
val take : when_negative_length:'err -> int -> 'a t -> ('a t, 'err) result
val drop : when_negative_length:'err -> int -> 'a t -> ('a t, 'err) result
module E :
Seqes.Sigs.SEQMON2TRAVERSORS
with type ('a, 'e) mon := ('a, 'e) result
with type ('a, 'e) callermon := ('a, 'e) result
with type ('a, 'e) t := 'a Stdlib.Seq.t
module S :
Seqes.Sigs.SEQMON1TRAVERSORS
with type 'a mon := 'a Lwt.t
with type 'a callermon := 'a Lwt.t
with type 'a t := 'a Stdlib.Seq.t
module ES :
Seqes.Sigs.SEQMON2TRAVERSORS
with type ('a, 'e) mon := ('a, 'e) result Lwt.t
with type ('a, 'e) callermon := ('a, 'e) result Lwt.t
with type ('a, 'e) t := 'a Stdlib.Seq.t
(** {3 Lwtreslib-specific extensions} *)
(** Similar to {!iter} but wraps the iteration in [result Lwt.t]. All the
steps of the iteration are started concurrently. The promise [iter_ep]
resolves once all the promises of the traversal resolve. At this point it
either:
- is rejected if at least one of the promises is, otherwise
- is fulfilled with [Error _] if at least one of the promises is,
otherwise
- is fulfilled with [Ok ()] if all the promises are. *)
val iter_ep :
('a -> (unit, 'trace) result Lwt.t) ->
'a t ->
(unit, 'trace list) result Lwt.t
(** Similar to {!iter} but wraps the iteration in {!Lwt}. All the
steps of the iteration are started concurrently. The promise [iter_p f s]
is resolved only once all the promises of the iteration are. At this point
it is either fulfilled if all promises are, or rejected if at least one of
them is. *)
val iter_p : ('a -> unit Lwt.t) -> 'a t -> unit Lwt.t
(** Similar to {!iteri} but wraps the iteration in [result Lwt.t]. All the
steps of the iteration are started concurrently. The promise [iteri_ep]
resolves once all the promises of the traversal resolve. At this point it
either:
- is rejected if at least one of the promises is, otherwise
- is fulfilled with [Error _] if at least one of the promises is,
otherwise
- is fulfilled with [Ok ()] if all the promises are. *)
val iteri_ep :
(int -> 'a -> (unit, 'trace) result Lwt.t) ->
'a t ->
(unit, 'trace list) result Lwt.t
(** Similar to {!iteri} but wraps the iteration in {!Lwt}. All the
steps of the iteration are started concurrently. The promise [iter_p f s]
is resolved only once all the promises of the iteration are. At this point
it is either fulfilled if all promises are, or rejected if at least one of
them is. *)
val iteri_p : (int -> 'a -> unit Lwt.t) -> 'a t -> unit Lwt.t
(** Similar to {!iter2} but wraps the iteration in [result Lwt.t]. All the
steps of the iteration are started concurrently. The promise
[iter2_ep f s1 s2] resolves once all the promises of the traversal resolve.
At this point it is either:
- rejected if at least one of the promises is,
- fulfilled with [Error _] if at least one of the promises is,
- fulfilled with [Ok ()] if all of the promises are.
Note that similarly to {!Stdlib.Seq.iter2} this function iterates on the
common-length prefix of the two sequences. As a result, the iteration can
be successful even if the two sequences are of different lengths. *)
val iter2_ep :
('a -> 'b -> (unit, 'trace) result Lwt.t) ->
'a t ->
'b t ->
(unit, 'trace list) result Lwt.t
(** Similar to {!iter2} but wraps the iteration in {!Lwt}. All the
steps of the iteration are started concurrently. The promise
[iter2_p f s1 s2] resolves once all the promises of the traversal resolve.
At this point it is either:
- rejected if at least one of the promises is,
- fulfilled with [()] if all of the promises are.
Note that similarly to {!Stdlib.Seq.iter2} this function iterates on the
common-length prefix of the two sequences. As a result, the iteration can
be successful even if the two sequences are of different lengths. *)
val iter2_p : ('a -> 'b -> unit Lwt.t) -> 'a t -> 'b t -> unit Lwt.t
end