A simple lazy list module for Lua.
local stream = require 'stream'
-- Define a Fibonacci sequence.
local fibs
fibs = stream.cons(0, stream.new(1, function()
return stream.zip(fibs, fibs.tail):map(function(x)
return x[1] + x[2]
end)
end))
-- Take the first 32 elements and print them.
fibs:take(32):walk(print)
More examples refer to tests.
Creates a stream with head
and tail
.
tail
must be a function that generates the tail of this stream.
Prepends the element x
to the head of the stream s
.
Creates a stream using the iteration function f
. x
and i
is the arguments of f
.
Returns a stream of tables, where the i-th table contains the i-th element from each of the argument streams. The returned stream is truncated in length to the length of the shortest argument stream.
Creates an infinite stream with all its elements are x
.
Creates an infinite incremental sequence starting with i
.
Returns whether all elements of the stream match the given f
function.
Returns whether any element of the stream matches the given f
function.
Applies the f
function to each element of the stream, threading an accumulator argument a
through the computation.
Calculates the elements of the stream immediately, no lazy.
Applies the given f
function to each element of the stream.
Applies the given f
function to each element of the stream and returns the stream.
Filters the elements of the stream to match the given f
function and returns the stream.
If x
is a integer, then takes the first x
elements.
If x
is a function, then takes all elements of the stream as long as x
is true.
If x
is a integer, then rejects the first x
elements.
If x
is a function, then rejects all elements of the stream as long as x
is true.
If x
is a integer, then rejects the last x
elements.
If x
is a function, then rejects all elements of the stream (start with the last one) as long as x
is true.