module type COMMON =sig
..end
type 'a
t
type 'a
co_t
exception Failure
Shtream.COMMON.t
. Callbacks such as the argument to Shtream.COMMON.from
or Shtream.COMMON.map
may raise this to indicate the end of the
shtream they are producing.exception CoFailure
val from : (int -> 'a option) -> 'a t
Some v
to produce v
or None
to end the shtream. The
function may also use Shtream Error Handling .
val close : 'a t -> unit
val of_list : 'a list -> 'a t
val list_of : 'a t -> 'a list
val of_stream : 'a Stream.t -> 'a t
Stream.t
to a shtream.val stream_of : 'a t -> 'a Stream.t
Stream.t
.val npeek : ?n:int -> 'a t -> 'a list
n
(default 1
) elements of a shtream.
If there are fewer than n
elements remaining, returns only
those.
Leaves the elements in the shtream.val peek : ?n:int -> 'a t -> 'a option
n
th (default 0
th) element of a shtream. Returns
Some v
if v
is the n
th zero-indexed element, and None
if
the shtream has n
or fewer elements remaining. Leaves
elements in the shtream.val empty : 'a t -> unit
val is_empty : 'a t -> bool
End_of_file
. The element is not
discarded.val status : 'a t -> Proc.status option
None
. Otherwise, Some
(Proc.WEXITED 0)
indicates normal termination and Some n
for
non-zero n
indicates abnormal termination. If a shtream was made
from an external process (via Shtream.of_command
, for example),
then n
is the exit code of the process. (If the process closes its
output and continues running, the shtream will terminate with Some
0
rather than wait for the process.)val junk : ?n:int -> 'a t -> unit
n
(default 1) elements of a shtream. If
fewer than n
remain, discards them all.val next : 'a t -> 'a
Shtream.COMMON.Failure
.val next' : 'a t -> 'a option
None
.val iter : ('a -> unit) -> 'a t -> unit
val filter : ('a -> bool) -> 'a t -> 'a t
Shtream.filter pred s
returns a new
shtream containing all the elements of s
that satisfy pred
.
The order of the elements is preserved, and s
becomes
invalid.val map : ('a -> 'b) -> 'a t -> 'b t
val concat_map : ('a -> 'b list) -> 'a t -> 'b t
Shtream.concat_map f s
applies f
to each element of s
in
turn, concatenating the resulting lists to form a new shtream.
The old shtream s
becomes invalid.val fold_left : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b
Shtream.fold_left f z s
applies the function f
to each
element of s
in turn, with an accumulating parameter
that starts at z
, and then returns the accumulated value.
Isomorphic to List.fold_left
.val fold_right : ('a -> 'b Lazy.t -> 'b) -> 'a t -> 'b -> 'b
Shtream.fold_right f s z
applies f
to each element
of s
and a lazy value that, when forced, returns the
rest of the fold. At the end of the shtream, the lazy value
returns z
. Isomorphic to call-by-need List.fold_right
.val nil : unit -> 'a t
val insert : 'a -> 'a t -> unit
val cons : 'a -> 'a t -> 'a t
val append : 'a t -> 'a t -> 'a t
Shtream.append s1 s2
returns a shtream
that first produces the elements of s1
, and should s1
be
exhausted, then produces the elements of s2
. This operation
invalidates both s1
and s2
.Because a shtream may be used far from its creation site, the client code of a shtream may be ill prepared to handle any exceptions the shtream might raise. To this end, we provide a simple API for signaling and handling shtream errors.
Here are several functions that a shtream generator (for
example, the callback given to Shtream.COMMON.from
or a reader) may call to
signal a condition:
val try_again : unit -> 'a
val warn : ('a, unit, string, 'b) Pervasives.format4 -> 'a
val fail_with : Proc.status -> 'a
fail_with (Proc.WEXITED 0)
is equivalent to raising
Shtream.COMMON.Failure
. Calling fail_with st
results in an exit status of
st
.typeerror_handler =
[ `Exception of exn | `Warning of string ] -> unit
warn s
then the error handler receives `Warning s
. If
a shtream generator raises an exception e
, then the error_handler
receives `Exception e
.val current_error_handler : error_handler Pervasives.ref
Shtream.COMMON.ignore_errors
or a user-defined function.val ignore_errors : error_handler
val warn_on_errors : error_handler
stderr
and continue evaluating the shtream.val die_on_errors : error_handler
stderr
terminate the shtream.val die_silently_on_errors : error_handler
A shtream is a source of typed data; a coshtream, of course, is a
sink. Given a function f
that consumes a shtream, Shtream.COMMON.coshtream_of
returns a coshtream handle that can be used to supply shtream
elements to f
asynchronously.
Because coshtreams work by calling f
in a child process, they
must marshal shtream data over a pipe; thus, they work only if
the element type is serializable using Marshal
. Moreover,
internal side effects in f
, such as mutating a ref cell, will not
be visible, since f
is called in a separate process.
val coshtream_of : ?procref:Channel.procref ->
('a t -> 'b) -> 'a co_t
coshtream_of f
spawns a child process in which it invokes f
,
and relays values sent to the resulting coshtream into the
shtream given to f
. If procref
is provided, the child
Proc.t
is stashed therein.val conil : unit -> 'a co_t
val conext : 'a co_t -> 'a -> unit
val coclose : 'a co_t -> unit
val annihilate : 'a t -> 'a co_t -> unit
val from_low : ?close:(unit -> unit) -> (int -> 'a) -> 'a t
Shtream.COMMON.from
, except that the function
returns 'a
rather than 'a option
, and must signal the end of
the shtream by raising Shtream.COMMON.Failure
. If close
is provided, it
will be called when the resulting shtream is closed; this could
be useful to free resources associated with the shtream.val claim : 'a t -> 'a t
claim s
returns
a shtream behaving identically to s
, while modifying s
to
contain no more data. This is useful for functions that want to
lay claim to a shtream argument while consuming it lazily.val set_reader : 'a t -> (Pervasives.in_channel -> 'a) -> unit
Shtream.of_channel
, for
example), this causes it to begin using the supplied reader to
produce shtream elements.val hint_reader : 'a t -> Reader.t -> unit
Shtream.COMMON.set_reader
, but it only changes the reader if the current
reader was defaulted rather than supplied by the user. For
example, if a shtream is created with AnyShtream.S.of_channel
and the user does not supply its option ?reader
argument, then
the reader may be changed by the system using Shtream.COMMON.hint_reader
;
but if the user explictly specifies a reader, then Shtream.COMMON.hint_reader
has no effect.typeprotector =
Util.protector
Util.protector
val add_protection : protector -> 'a t -> unit
Shtream.COMMON.protector
is a function that, given a thunk, must return the value
of the thunk, but may perform some preparation first or cleanup
afterward. Adding a protector to a shtream means that any internal
shtream evalution will be performed in the dynamic context of the
protector. If more than one protector has been added to a shtream,
they will be performed with the newest protector on the outside.val add_cleanup : (unit -> unit) -> 'a t -> unit