A stream is created by functions stream which come with different argument types.
There is, however, also a constructor called new which creates an uninitialized stream. A new() stream should never be asked for elements. That function is mainly provided to allow recursive definitions of structures like in the following code.
T ==> Integer;
S ==> DataStream T;
import from T, S;
(x: S) + (y: S): S == stream(a+b for a in x for b in y);
(x: S) * (y: S): S == {
local times(n: I): T == {
t: T := 0;
for k in 0..n | not zero?(xk := x.k) repeat {
t := t + xk * y.(n-k);
}
t;
}
stream(times n for n: I in 0..);
}
s: DataStream T := new();
1: S := stream(generator [1, 0]);
x: S := stream(generator [0, 1, 0]);
set!(s, generator(1 + x * s * s));
l1: List Integer := [1,1,2*2,5*6,14*24,42*120];
l2: List Integer := [i for i in l1 for t in s];
Export of DataStream
stream: T -> %
Usage
T == Integer;
t: T := 7;
s: DataStream T := stream(t);
a: MachineInteger := #s;
s.1 := 0;
b: MachineInteger := #s;
Description
Construct a constant stream.
This function constructs a stream that for each i gives si = t. In the example code above we get (7,7,7,7,…) which would become (7,0,7,7,…) after the last assignment.
The values of a and b are 1 and 3 respectively.
Export of DataStream
stream: Generator T -> %
Usage
T == Integer;
g: Generator T := generator [2,4,6,7];
s: DataStream T := stream g;
Parameters
A (finite or infinite) generator that computes the next values of the stream.
Description
Construct a stream.
The function constructs the stream
(169) |
[x for x in g]
In case the generator g is finite and generates n elements then the result is
(170) |
Remarks
It is allowed to call stream with a generator that does not generate any element, but the resulting stream may not be ask to reveal any of its entries, since there are none. It is basically the same to say stream(generate) and new().
Export of DataStream
stream: (Generator T, T) -> %
Usage
g: Generator Integer := generator [2,4,6,7];
c: Integer := 3;
s: DataStream Integer := stream(g, c);
Description
stream(g, c) is an abbreviation for
stream(generate {for t in g repeat yield t; yield c});
Export of DataStream
stream: ((MachineInteger, %) -> T) -> %
Usage
macro T == Integer;
h(n: I, s: DataStream T): T == {
n < 2 => 1;
s(n-1) + s(n-2);
}
s: DataStream Integer := stream(h);
Description
Construct a stream.
The function h that is given as a parameter is assumed to compute the n-th value of the stream depending on some or all previous entries of the stream. Thus, s:=stream(h) can be seen as encoding for every n
(171) |
Remarks
If h(n,s) is called, it is only assured that the stream is initialized for the values s0,…,sn−1. h(n,s) may not access sk for k ≥ n.
Export of DataStream
new: () -> %
Usage
Give an recursion example!
Description
Construct an uninitialized stream.
new() is an abbreviation for stream(generator empty$List(T)).