Module BatStream.StreamLabels


module StreamLabels: sig .. end


Note 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.

Stream iterators

val iter : f:('a -> unit) -> 'a Stream.t -> unit
Stream.iter f s scans the whole stream s, applying function f in turn to each stream element encountered.
val foldl : f:('a -> 'b -> 'a * bool option) -> init:'a -> 'b Stream.t -> 'a
foldl 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 : f:('a -> 'b lazy_t -> 'b) -> init:'b -> 'a Stream.t -> 'b
foldr 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 determin the result by itself.
val fold : f:('a -> 'a -> 'a * bool option) -> init:'a Stream.t -> 'a
fold 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.
val filter : f:('a -> bool) -> 'a Stream.t -> 'a Stream.t
filter test stream picks all the elements satisfying test from stream and return the results in the same order as a stream.

Computation over stream

All the functions in this part are lazy.

val map : f:('a -> 'b) -> 'a Stream.t -> 'b Stream.t
map f stream applies f in turn to elements from stream and return the results as a stream in the same order.
val map2 : f:('a -> 'b -> 'c) -> 'a Stream.t -> 'b Stream.t -> 'c Stream.t
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.
val scanl : f:('a -> 'b -> 'a) -> 'a -> 'b Stream.t -> 'a Stream.t
scanl f init stream returns a stream of successive reduced values from the left: scanl f init [<'e1;'e2;..>] = [<'init; '((f init e1) as e1'); '(f e1' e2); ..>]
val scan : f:('a -> 'a -> 'a) -> 'a Stream.t -> 'a Stream.t
scan is similar to scanl but without the init value: scan f [<'e1;'e2;..>] = [<'e1;'(f e1 e2);..>].
val take_while : f:('a -> bool) -> 'a Stream.t -> 'a Stream.t
take_while test stream returns the longest (possibly empty) prefix of stream of elements that satisfy test.
val drop_while : f:('a -> bool) -> 'a Stream.t -> 'a Stream.t
drop_while test stream returns the remaining suffix of take_while test stream.

Streams pair arithmetic

All the functions in this part are lazy.

val merge : f:(bool -> 'a -> bool) -> 'a Stream.t * 'a Stream.t -> 'a Stream.t
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.
val switch : f:('a -> bool) -> 'a Stream.t -> 'a Stream.t * 'a Stream.t
switch ~f: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.