rapport/enumeration.lua

7 lines
500 B
Lua
Raw Normal View History

--Lazy enumerator of countable infinite set.
--Pass a function closure for generating the set element-by-element, in order.
--This function can return a "simple" value followed by an "exact" value
--Returns a table including:
-- an array with all elements generated so far (grows as needed upon access)
local enum = { __index = function( t, n ) for i = #t + 1, n do t[i] = t.next() end return rawget(t, n) end }
return function(nextElement) return setmetatable( { ["next"] = nextElement }, enum ) end