Skip to content

Instantly share code, notes, and snippets.

@SoniEx2
Created January 31, 2017 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SoniEx2/be195240141fa1400dfe72c673e68bf3 to your computer and use it in GitHub Desktop.
Save SoniEx2/be195240141fa1400dfe72c673e68bf3 to your computer and use it in GitHub Desktop.
-- 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