Skip to content

Instantly share code, notes, and snippets.

@rtsisyk
Created November 21, 2013 16:11
Show Gist options
  • Select an option

  • Save rtsisyk/7584571 to your computer and use it in GitHub Desktop.

Select an option

Save rtsisyk/7584571 to your computer and use it in GitHub Desktop.
local filterm_gen
local filterm_gen_shrink
-- A helper for case 2
filterm_gen_shrink = function(fun, gen_x, param_x, state_x)
return filterm_gen(fun, gen_x, param_x, gen_x(param_x, state_x))
end
filterm_gen = function(fun, gen_x, param_x, state_x, ...)
if state_x == nil then return nil end
if fun(...) then return state_x, ... end
-- CASE 1 (do not works):
return filterm_gen(fun, gen_x, param_x, gen_x(param_x, state_x))
--[[
[TRACE 1 test.lua:41 loop]
[TRACE 2 test.lua:46 return]
[TRACE --- (2/1) test.lua:46 -- NYI: bytecode 71 at test.lua:13]
[TRACE --- (2/1) test.lua:46 -- NYI: bytecode 71 at test.lua:13]
[TRACE --- (2/1) test.lua:46 -- NYI: bytecode 71 at test.lua:13]
[TRACE --- (2/1) test.lua:46 -- NYI: bytecode 71 at test.lua:13]
[TRACE 3 (2/1) test.lua:46 -- fallback to interpreter]
--]]
-- CASE 2 (works):
--return filterm_gen_shrink(fun, gen_x, param_x, state_x)
--[[
[TRACE 1 test.lua:44 loop]
[TRACE 2 test.lua:49 return]
[TRACE 3 test.lua:4 tail-recursion]
[TRACE 4 (3/6) test.lua:49 -> 3]
--]]
end
local filter_gen = function(param, state_x)
local fun, gen_x, param_x = param[1], param[2], param[3]
return filterm_gen(fun, gen_x, param_x, gen_x(param_x, state_x))
end
n = 1000
t = { [1] = 1, [n] = n }
for i=1,n do
t[i] = i
end
local gen_x, param_x, state_x = ipairs(t)
local function fun(x) return x % 64 == 0 end
for _it, a in filter_gen, {fun, gen_x, param_x}, state_x do
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment