Skip to content

Instantly share code, notes, and snippets.

@itsbth
Created December 22, 2011 10:06
Show Gist options
  • Select an option

  • Save itsbth/1509787 to your computer and use it in GitHub Desktop.

Select an option

Save itsbth/1509787 to your computer and use it in GitHub Desktop.
local function map(f, iter, state, k)
return function(state, k)
local v
k, v = iter(state, k)
if k then
return k, f(v)
end
end, state, k
end
local function identity(x) return x end
local tbl = {}
for i = 1, 10^6 do
tbl[#tbl + 1] = i
end
for k, v in map(identity, ipairs(tbl)) do
-- nothing
end
local ipairs = ipairs
local function ipairs_map(tbl, mapfunction)
local iter, state, k = ipairs(tbl)
return function(state, k)
local v
k,v = iter(state, k)
if k == nil then return nil end
return k,mapfunction(v)
end, state, k
end
local function identity(x) return x end
local tbl = {}
for i = 1, 10^6 do
tbl[#tbl + 1] = i
end
for k, v in ipairs_map(tbl, identity) do
-- nothing
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment