[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Mapping values from one table to another
- From: "Greg Bakker" <gbakker@...>
- Date: Mon, 12 Mar 2007 23:46:13 +0000
Hi,
I'm curious as to how best to create a table via a direct mapping from another.
For example, say you want
{ 1, 2, 3 } => { {name="something1", value=1}, {name="something2", value=2}, {name="something3", value=3} }
Currently I'm using
function map(f, v, ...)
if v then
return f(v), map(f, ...)
end
end
function func(v) return {name="something"..v, value=v} end
a = { 1, 2, 3 }
b = { map(func, unpack(a)) }
which is probable stack abuse. Is there a canonical approach; maybe a better way is to use a loop and insert-at-end?
Thanks,
Greg Bakker