
% Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd.
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
%     - Redistributions of source code must retain the above copyright
%       notice, this list of conditions and the following disclaimer.
%
%     - Redistributions in binary form must reproduce the above copyright
%       notice, this list of conditions and the following disclaimer in
%       the documentation and/or other materials provided with the
%       distribution.
%
%     - Neither the name of The Numerical ALgorithms Group Ltd. nor the
%       names of its contributors may be used to endorse or promote products
%       derived from this software without specific prior written permission.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
% IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
% TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
% OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES-- LOSS OF USE, DATA, OR
% PROFITS-- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



A \spadtype{Stream} object is represented as a list whose last element contains the
wherewithal to create the next element, should it ever be required.
\xtc{
Let \spad{ints} be the infinite stream of non-negative integers.
}{
\spadcommand{ints := [i for i in 0..] \bound{ints}}
}
By default, ten stream elements are calculated.
This number may be changed to something else by the system command
\spadcmd{)set streams calculate}.
\syscmdindex{set streams calculate}
For the display purposes of this book, we have chosen a smaller value.
\xtc{
More generally, you can construct a stream by specifying its initial
value and a function which, when given an element, creates the next element.
}{
\spadcommand{f : List INT -> List INT \bound{fdec}}
}
\xtc{
}{
\spadcommand{f x == [x.1 + x.2, x.1] \bound{f}\free{fdec}}
}
\xtc{
}{
\spadcommand{fibs := [i.2 for i in [stream(f, [1, 1])]] \bound{fibs}\free{f}}
}
\xtc{
You can create the stream of odd non-negative integers by either filtering
them from the integers, or by evaluating an expression for each integer.
}{
\spadcommand{[i for i in ints | odd? i] \free{ints}}
}
\xtc{
}{
\spadcommand{odds := [2*i+1 for i in ints]\bound{odds}\free{ints}}
}
\xtc{
You can accumulate the initial segments of a stream using the
\spadfunFrom{scan}{StreamFunctions2} operation.
}{
\spadcommand{scan(0,+,odds) \free{odds}}
}
\xtc{
The corresponding elements of
two or more streams can be combined in this way.
}{
\spadcommand{[i*j for i in ints for j in odds]\free{ints} \free{odds}}
}
\xtc{
}{
\spadcommand{map(*,ints,odds)\free{ints odds}}
}
\xtc{
Many operations similar to those applicable to lists are available for
streams.
}{
\spadcommand{first ints \free{ints}}
}
\xtc{
}{
\spadcommand{rest ints \free{ints}}
}
\xtc{
}{
\spadcommand{fibs 20 \free{fibs}}
}
The packages \spadtype{StreamFunctions1},
\exptypeindex{StreamFunctions1}
\spadtype{StreamFunctions2} and
\exptypeindex{StreamFunctions2}
\spadtype{StreamFunctions3} export some useful stream manipulation
operations.
\exptypeindex{StreamFunctions3}
For more information, see
\spadref{ugLangIts},
\spadref{ugProblemSeries},
\xmpref{ContinuedFraction}, and
\xmpref{List}.
%
\showBlurb{Stream}
