This file implements a stream data structure which is tested in the file test/stream.as.nw
A stream models a function ℕ → T for some type T where it caches all the elements up to the index of the last element that has ever been accessed. In fact, a DataStream can be seen as an infinite array.
The domain DataStream resembles in many aspects the functionality of the Stream domain from the Aldor library, but we had to implement our own stream type since the functionality of the Aldor stream was not sufficient and could not be extended without knowing the internal representation of Stream.
Since we are going to document our DataStream more extensively, it might even become a replacement for the Aldor Stream.
In order to make the description of a DataStream finite, one can think of a DataStream as a pair (a,g) where a is a finite array and g is a function that computes the next elements.
Like the domain Stream from the Aldor library, DataStream handles efficiently infinite streams that are constant from some point on by storing the constant value only once.
Usage
T == Integer
s: DataStream T := stream 0;
s := stream(generator [1, 3]);
g: Generator T == generate for i in 2.. repeat yield i;
s := stream g;
Description
Implements an infinite array.
The domain DataStream can be abstractly seen as an infinite array. However, to make this data structure finite, it is more appropriate to view it as a pair (a,g) of an Array(T) and a Generator(T).
Like Array, DataStream is 0-based.
Exports of DataStream
stream: T -> % Construct a constant stream.
stream: Generator T -> % Construct a stream.
stream: (Generator T, T) -> % Construct a stream.
stream: ((MachineInteger, %) -> T) -> % Construct a stream.
new: () -> % Construct an uninitialized stream.
#: % -> MachineInteger Number of computed elements.
data: % -> PrimitiveArray T Returns the array of computed elements.
constant?: % -> Boolean Returns true if the stream is known to be eventually constant.
generator: % -> Generator T Generate all elements of the stream.
elements: (%, startFromIndex: MachineInteger == 0) -> Generator T Generate all elements of the stream until the last elements repeats.
apply: (%, MachineInteger) -> T Access an entry of the stream.
set!: (%, MachineInteger, T) -> T Destructively modify an entry of a stream.
set!: (%, Generator T) -> () Construct a stream using an old structure.
map: (T -> T) -> % -> % Maps a function on a stream.