Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
38
Task/Generator-Exponential/Lua/generator-exponential.lua
Normal file
38
Task/Generator-Exponential/Lua/generator-exponential.lua
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
--could be done with a coroutine, but a simple closure works just as well.
|
||||
local function powgen(m)
|
||||
local count = 0
|
||||
return function()
|
||||
count = count + 1
|
||||
return count^m
|
||||
end
|
||||
end
|
||||
|
||||
local squares = powgen(2)
|
||||
local cubes = powgen(3)
|
||||
|
||||
local cowrap,coyield = coroutine.wrap, coroutine.yield
|
||||
|
||||
local function filter(f,g)
|
||||
return cowrap(function()
|
||||
local ff,gg = f(), g()
|
||||
while true do
|
||||
if ff == gg then
|
||||
ff,gg = f(), g()
|
||||
elseif ff < gg then
|
||||
coyield(ff)
|
||||
ff = f()
|
||||
else
|
||||
gg = g()
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
filter = filter(squares,cubes)
|
||||
|
||||
for i = 1,30 do
|
||||
local result = filter()
|
||||
if i > 20 then
|
||||
print(result)
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue