#### 12.2 Constructor Functions of DataStream

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];

Note that the the two lists at the end appear to be equal. See also test case testStream10.

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.

392aexports: DataStream 392a  (386)  394a
stream: T -> %;
392bimplementation: DataStream 392b  (386)  394b
stream(t: T): % == {
g: Generator T == generate yield t;
per [g, [t], 1, 1, true];
}

Uses Generator 617.

Export of DataStream

stream: Generator T -> %

Usage

T == Integer;
g: Generator T := generator [2,4,6,7];
s: DataStream T := stream g;

Parameters

g: Generator Integer

A (finite or infinite) generator that computes the next values of the stream.

Description

Construct a stream.

The function constructs the stream

 (169)
or (informally) the infinite array

[x for x in g]

where gi is the i-th element generated by the generator 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().

394aexports: DataStream 392a+   (386)  392a  395a
stream: Generator T -> %;

Uses Generator 617.
394bimplementation: DataStream 392b+   (386)  392b  395b
stream(g: Generator T): % == per [g, empty, 0, 0, false];

Uses Generator 617.

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

Construct a stream.

stream(g, c) is an abbreviation for

stream(generate {for t in g repeat yield t; yield c});

395aexports: DataStream 392a+   (386)  394a  396
stream: (Generator T, T) -> %;

Uses Generator 617.
395bimplementation: DataStream 392b+   (386)  394b  397
stream(g: Generator T, c: T): % == {
stream(generate {for t in g repeat yield t; yield c});
}

Uses Generator 617.

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,,sn1. h(n,s) may not access sk for k n.

396exports: DataStream 392a+   (386)  395a  398a
stream: ((MachineInteger, %) -> T) -> %;

Uses MachineInteger 67.
397implementation: DataStream 392b+   (386)  395b  398b
stream(f: (I, %) -> T): % == {
s: % := new();
set!(s, generate for n in 0.. repeat yield f(n, s));
s;
}

Uses I 47.

Export of DataStream

new: () -> %

Usage

Give an recursion example!

Description

Construct an uninitialized stream.

new() is an abbreviation for stream(generator empty\$List(T)).

398aexports: DataStream 392a+   (386)  396  401a
new: () -> %;
398bimplementation: DataStream 392b+   (386)  397  401b
--local emptyList: List T == [];
new(): % == {import from List T; stream generator empty}