package batteries
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=e4b70d1a716f0aaba36f419f618d0a2e
sha512=a31f1f8cf2c7c3c6c757f3bfae98ff61bb32bab6a1f1e215937df42bcfa447aad41a37edb28d7bcecb88b3838ed8bd57142bcf8e2d28e09bb538055fd8a3b72d
doc/batteries.unthreaded/BatStream/index.html
Module BatStream
Streams and stream parsers
Streams are a read-and-forget data structure, comparable to enumerations. In Batteries Included, streams are deprecated in favor of enumerations, defined in module BatEnum.
Note This module is provided essentially for backwards-compatibility. If you feel like using Stream.t, please take a look at BatEnum or LazyList.
This module is based on Zheng Li's SDFlow
This module replaces Stdlib's Stream module.
@documents Stream
type 'a t = 'a Stream.tinclude BatEnum.Enumerable with type 'a enumerable = 'a t
type 'a enumerable = 'a tThe data structure, e.g. 'a List.t
include BatInterfaces.Mappable with type 'a mappable = 'a t
type 'a mappable = 'a tThe data structure, e.g. 'a List.t
The type of streams holding values of type 'a.
Raised by parsers when none of the first components of the stream patterns is accepted.
Raised by parsers when the first component of a stream pattern is accepted, but one of the following components is rejected.
Stream builders
val from : (int -> 'a option) -> 'a tStream.from f returns a stream built from the function f. To create a new stream element, the function f is called with the current stream count. The user function f must return either Some <value> for a value or None to specify the end of the stream.
val of_list : 'a list -> 'a tReturn the stream holding the elements of the list in the same order.
val of_string : string -> char tReturn the stream of the characters of the string parameter.
val of_channel : in_channel -> char tReturn the stream of the characters read from the input channel.
Other Stream builders
Warning: these functions create streams with fast access; it is illegal to mix them with streams built with [< >]; would raise Failure when accessing such mixed streams.
val of_fun : (unit -> 'a) -> 'a tStream.of_fun f returns a stream built from the function f. To create a new stream element, the function f is called with the current stream count. The user function f must return either Some <value> for a value or None to specify the end of the stream.
Stream iterator
val iter : ('a -> unit) -> 'a t -> unitStream.iter f s scans the whole stream s, applying function f in turn to each stream element encountered.
val foldl : ('a -> 'b -> 'a * bool option) -> 'a -> 'b t -> 'afoldl f init stream is a lazy fold_left. f accu elt should return (new_accu, state) where new_accu is normal accumulation result, and state is a flag representing whether the computation should continue and whether the last operation is valid: None means continue, Some b means stop where b = true means the last addition is still valid and b = false means the last addition is invalid and should be revert.
val foldr : ('a -> 'b lazy_t -> 'b) -> 'b -> 'a t -> 'bfoldr f init stream is a lazy fold_right. Unlike the normal fold_right, the accumulation parameter of f elt accu is lazy, hence it can decide not to force the evaluation of accu if the current element elt can determine the result by itself.
val fold : ('a -> 'a -> 'a * bool option) -> 'a t -> 'afold is foldl without initialization value, where the first element of stream is taken as init. It raises End_of_stream exception when the input stream is empty.
filter test stream picks all the elements satisfying test from stream and return the results in the same order as a stream.
Predefined parsers
val empty : 'a t -> unitReturn () if the stream is empty, else raise Stream.Failure.
Useful functions
val peek : 'a t -> 'a optionReturn Some of "the first element" of the stream, or None if the stream is empty.
val junk : 'a t -> unitRemove the first element of the stream, possibly unfreezing it before.
val count : 'a t -> intReturn the current count of the stream elements, i.e. the number of the stream elements discarded.
val npeek : int -> 'a t -> 'a listnpeek n returns the list of the n first elements of the stream, or all its remaining elements if less than n elements are available.
Conversion functions
Convert a stream to an enumeration. Reading the resulting enumeration will consume elements from the stream. This is the preferred manner of converting from a stream to any other data structure.
Convert an enumeration to a stream. Reading the resulting stream will consume elements from the enumeration. This is the preferred manner of creating a stream.
val of_input : BatIO.input -> char tConvert an input to a stream.
val to_list : 'a t -> 'a listConvert a stream to a list
val to_string : char t -> stringconvert stream of chars to string, using buffer
convert stream to string, using Printf with given format
val to_string_fun : ('a -> string) -> 'a t -> stringconvert stream to string, using given conversion function
Stream consumers
val on_output : 'a BatIO.output -> char t -> unitConvert an output to a stream.
Computation over stream
All the functions in this part are lazy.
map f stream applies f in turn to elements from stream and return the results as a stream in the same order.
map2 f streama streamb applies f in turn to elements of corresponding positions from streama and streamb. The results are constructed in the same order as a stream. If one stream is short, excess elements of the longer stream are ignored.
scanl f init stream returns a stream of successive reduced values from the left: scanl f init [< 'e0; 'e1; ... >] is equivalent to [< 'init; '(f init e0); '(f (f init e0) e1); ... >]
scan is similar to scanl but without the init value: scanl f init [< 'e0; 'e1; 'e2; ... >] is equivalent to [< 'e0; '(f e0 e1); '(f (f e0 e1) e2); ... >]
take n stream returns the prefix of stream of length n, or stream itself if n is greater than the length of stream
drop n stream returns the suffix of stream after the first n elements, or a empty stream if n is greater than the length of stream
take_while test stream returns the longest (possibly empty) prefix of stream of elements that satisfy test.
drop_while test stream returns the remaining suffix of take_while test stream.
Streams pair arithmetic
All the functions in this part are lazy.
dup stream returns a pair of streams which are identical to stream. Note that stream is a destructive data structure, the point of dup is to return two streams can be used independently.
NOT IMPLEMENTED CORRECTLY - WILL RAISE Failure UNTIL CORRECT IMPLEMENTATION FOUND
comb transform a pair of stream into a stream of pairs of corresponding elements. If one stream is short, excess elements of the longer stream are ignored.
merge test (streama, streamb) merge the elements from streama and streamb into a single stream. The bool type here represents the id of the two input streams where true is the first and false represents the second. The test function is applied to each element of the output stream together with the id of the input stream from which it was extracted, to decide which stream should the next element come from. The first element is always taken from streama. When a stream runs out of elements, the merge process will continue to take elements from the other stream until both streams reach their ends.
switch test stream split stream into two streams, where the first stream have all the elements satisfying test, the second stream is opposite. The order of elements in the source stream is preserved.
Stream arithmetic
All the functions in this part are lazy.
val is_empty : 'a t -> boolis_empty stream tests whether stream is empty. But note that it forces the evaluation of the head element if any.
Predefined parsers
val next : 'a t -> 'aReturn the first element of the stream and remove it from the stream.
module StreamLabels : sig ... endNote This module is provided essentially for backwards-compatibility. If you feel like using Stream.t, please take a look at BatEnum or LazyList and GenParser.