Revision: 2021
at December 18, 2006 08:21 by kergoth

Updated Code
```require "std.table" -- For memoize

function map(t, f)
local nt = table.memoize(function (k) return f(t[k]) end)
local mt = getmetatable(nt)
function mt:__pairs()
local k
local function mynext()
k,v = next(t, k)
if k == nil then return nil end
return k,nt[k]
end
return mynext, self
end
return nt
end
require "std.base" -- For better table tostring()

local mylist = {1,3,5,7,9}
local newlist = map(mylist, function(v) return 3 * v - 2 end)
assert(mylist[3] == 5)
assert(newlist[3] == 13)

require "std.base" -- For the pairs() that obeys the __pairs metamethod
print("old:", mylist)
print("new(3*v-2):", newlist)```

Revision: 2020
at December 18, 2006 08:06 by kergoth

Initial Code
```require "std.table" -- For memoize

function map(t, f)
local nt = table.memoize(function (k) return f(t[k]) end)
local mt = getmetatable(nt)
function mt:__pairs()
local k
local function mynext()
k,v = next(t, k)
if k == nil then return nil end
return k,nt[k]
end
return mynext, self
end
return nt
end
require "std.base" -- For better table tostring()

local mylist = {1,3,5,7,9}
local newlist = map(mylist, function(v) return 3 * v - 2 end)
assert(mylist[3] == 5)
assert(newlist[3] == 13)

require "std.base" -- For the pairs() that obeys the __pairs metamethod
print("old:", mylist)
print("new(3*v-2):", newlist)```

Initial URL

Initial Description

Initial Title
`Lua: Lazy Map Implementation: Round One`

Initial Tags

Initial Language
`Lua`