Module Lwt_seq
Contents
Instructions: Use this module in your project
In the IDE (CLion, Visual Studio Code, Xcode, etc.) you use for your DkSDK project:
Add the following to your project's
dependencies/CMakeLists.txt
:DkSDKProject_DeclareAvailable(lwt CONSTRAINT "= 5.6.1" FINDLIBS lwt lwt.unix) DkSDKProject_MakeAvailable(lwt)
Add the
Findlib::lwt
library to any desired targets insrc/*/CMakeLists.txt
:target_link_libraries(YourPackage_YourLibraryName # ... existing libraries, if any ... Findlib::lwt)
Click your IDE's
Build
button
Not using DkSDK?
FIRST, do one or all of the following:
Run:
opam install lwt.5.6.1
Edit your
dune-project
and add:(package (name YourExistingPackage) (depends ; ... existing dependenices ... (lwt (>= 5.6.1))))
Then run:
dune build *.opam # if this fails, run: dune build
Edit your
<package>.opam
file and add:depends: [ # ... existing dependencies ... "lwt" {>= "5.6.1"} ]
Then run:
opam install . --deps-only
FINALLY, add the lwt
library to any desired (library)
and/or (executable)
targets in your **/dune
files:
(library
(name YourLibrary)
; ... existing library options ...
(libraries
; ... existing libraries ...
lwt))
(executable
(name YourExecutable)
; ... existing executable options ...
(libraries
; ... existing libraries ...
lwt))
The type of delayed lists containing elements of type 'a
. Note that
the concrete list node 'a node
is delayed under a closure, not a
lazy
block, which means it might be recomputed every time we access
it.
and
``+'a node`` =
|
Nil
|
Cons
of
'a
*
'a
t
(* A fully-evaluated list node, either empty or containing an element and a delayed tail.
*)
val
empty :
'a
t
The empty sequence, containing no elements.
val
return :
'a
->
'a
t
The singleton sequence containing only the given element.
cons x xs
is the sequence containing the element x
followed by the
sequence xs
cons x xs
is the sequence containing the element promised by x
followed by the sequence xs
map f seq
returns a new sequence whose elements are the elements of
seq
, transformed by f
. This transformation is lazy, it only applies
when the result is traversed.
map_s f seq
is like map f seq
but f
is a function that returns a
promise.
Note that there is no concurrency between the promises from the
underlying sequence seq
and the promises from applying the function
f
. In other words, the next promise-element of the underlying sequence
(seq
) is only created when the current promise-element of the returned
sequence (as mapped by f
) has resolved. This scheduling is true for
all the _s
functions of this module.
Remove from the sequence the elements that do not satisfy the given predicate. This transformation is lazy, it only applies when the result is traversed.
filter_s
is like filter
but the predicate returns a promise.
See map_s
for additional details about scheduling.
Apply the function to every element; if f x = None
then x
is
dropped; if f x = Some y
then y
is returned. This transformation is
lazy, it only applies when the result is traversed.
filter_map_s
is like filter
but the predicate returns a promise.
See map_s
for additional details about scheduling.
Map each element to a subsequence, then return each element of this sub-sequence in turn. This transformation is lazy, it only applies when the result is traversed.
Traverse the sequence from left to right, combining each element with the accumulator using the given function. The traversal happens immediately and will not terminate (i.e., the promise will not resolve) on infinite sequences.
fold_left_s
is like fold_left
but the function returns a promise.
See map_s
for additional details about scheduling.
Iterate on the sequence, calling the (imperative) function on every element.
The sequence's next node is evaluated only once the function has
finished processing the current element. More formally: the promise for
the n+1
th node of the sequence is created only once the promise
returned by f
on the n
th element of the sequence has resolved.
The traversal happens immediately and will not terminate (i.e., the promise will not resolve) on infinite sequences.
iter_s
is like iter
but the function returns a promise.
See map_s
for additional details about scheduling.
Iterate on the sequence, calling the (imperative) function on every element.
The sequence's next node is evaluated as soon as the previous node is resolved.
The traversal happens immediately and will not terminate (i.e., the promise will not resolve) on infinite sequences.
iter_n ~max_concurrency f s
Iterates on the sequence s
, calling the (imperative) function f
on
every element.
The sum total of unresolved promises returned by f
never exceeds
max_concurrency
. Node suspensions are evaluated only when there is
capacity for f
-promises to be evaluated. Consequently, there might be
significantly fewer than max_concurrency
promises being evaluated
concurrently; especially if the node suspensions take longer to evaluate
than the f
-promises.
The traversal happens immediately and will not terminate (i.e., the promise will not resolve) on infinite sequences.
-
parameter max_concurrency
defaults to
1
. -
raises Invalid_argument
if
max_concurrency < 1
.
val
unfold : ``(
'b
->
``(
'a
*
'b
)`` option``)``
->
'b
->
'a
t
Build a sequence from a step function and an initial value. unfold f u
returns empty
if the promise f u
resolves to None
, or
fun () -> Lwt.return (Cons (x, unfold f y))
if the promise f u
resolves to Some (x, y)
.
unfold_lwt
is like unfold
but the step function returns a promise.
Convert a sequence to a list, preserving order. The traversal happens immediately and will not terminate (i.e., the promise will not resolve) on infinite sequences.
val
of_list :
'a
list``
->
'a
t
Convert a list to a sequence, preserving order.
val
of_seq :
'a
Stdlib.Seq.t
->
'a
t
Convert from 'a Stdlib.Seq.t
to 'a Lwt_seq.t
. This transformation is
lazy, it only applies when the result is traversed.
val
of_seq_lwt :
'a
Lwt.t
Stdlib.Seq.t
->
'a
t
Convert from 'a Lwt.t Stdlib.Seq.t
to 'a Lwt_seq.t
. This
transformation is lazy, it only applies when the result is traversed.