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 tSome v to produce v or None to end the shtream. The
function may also use Shtream Error Handling .
val close : 'a t -> unitval of_list : 'a list -> 'a tval list_of : 'a t -> 'a listval of_stream : 'a Stream.t -> 'a tStream.t to a shtream.val stream_of : 'a t -> 'a Stream.tStream.t.val npeek : ?n:int -> 'a t -> 'a listn (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 optionnth (default 0th) element of a shtream. Returns
Some v if v is the nth 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 -> boolEnd_of_file. The element is not
discarded.val status : 'a t -> Proc.status optionNone. 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 -> unitn (default 1) elements of a shtream. If
fewer than n remain, discards them all.val next : 'a t -> 'aShtream.COMMON.Failure.val next' : 'a t -> 'a optionNone.val iter : ('a -> unit) -> 'a t -> unitval filter : ('a -> bool) -> 'a t -> 'a tShtream.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 tval concat_map : ('a -> 'b list) -> 'a t -> 'b tShtream.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 -> 'bShtream.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 -> 'bShtream.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 tval insert : 'a -> 'a t -> unitval cons : 'a -> 'a t -> 'a tval append : 'a t -> 'a t -> 'a tShtream.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 -> 'aval warn : ('a, unit, string, 'b) Pervasives.format4 -> 'aval fail_with : Proc.status -> 'afail_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.refShtream.COMMON.ignore_errors or a user-defined function.val ignore_errors : error_handlerval warn_on_errors : error_handlerstderr and continue evaluating the shtream.val die_on_errors : error_handlerstderr 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_tcoshtream_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_tval conext : 'a co_t -> 'a -> unitval coclose : 'a co_t -> unitval annihilate : 'a t -> 'a co_t -> unitval from_low : ?close:(unit -> unit) -> (int -> 'a) -> 'a tShtream.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 tclaim 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) -> unitShtream.of_channel, for
example), this causes it to begin using the supplied reader to
produce shtream elements.val hint_reader : 'a t -> Reader.t -> unitShtream.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.protectorval add_protection : protector -> 'a t -> unitShtream.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