Created
January 31, 2017 02:11
-
-
Save SoniEx2/be195240141fa1400dfe72c673e68bf3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- This Lua stack may be faster than the vararg stack, since it does less copies. Haven't benchmarked it yet. | |
-- However, it can't hold as many elements (13 000 ish elements on my machine, vs uh, 100 000 or more with varargs?). | |
local function callstack() | |
local function f(v) | |
local op, nv = coroutine.yield() | |
while true do | |
if op == "push" then | |
op, nv = coroutine.yield(f(nv)) | |
elseif op == "pop" then | |
return v | |
end | |
end | |
end | |
local function empty() | |
local op, nv = coroutine.yield() | |
while true do | |
if op == "push" then | |
op, nv = coroutine.yield(f(nv)) | |
else | |
op, nv = coroutine.yield() | |
end | |
end | |
end | |
local co = coroutine.wrap(empty) | |
co() | |
return function(v) | |
co("push", v) | |
end, function() | |
return co("pop") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment