Functional Table Processing
Map Numbers
Transform each value in a table into a new value.
map values
Mapping applies the same transformation to every element.
Map Numbers
map_numbers.lua
Replay: real traced execution (multi-file project)
local factor = 2
local output = ""
for index, value in ipairs({1, 2, 3}) do
local mapped = value * factor
if index > 1 then
output = output .. ","
end
output = output .. mapped
end
print("factor=" .. factor)
print("output=" .. output)
local factor = 3
local output = ""
for index, value in ipairs({1, 2, 3}) do
local mapped = value * factor
if index > 1 then
output = output .. ","
end
output = output .. mapped
end
print("factor=" .. factor)
print("output=" .. output)
local factor = 4
local output = ""
for index, value in ipairs({1, 2, 3}) do
local mapped = value * factor
if index > 1 then
output = output .. ","
end
output = output .. mapped
end
print("factor=" .. factor)
print("output=" .. output)
factor ← 2, output ← (empty)
1local factor→ 2 = 2 --@factor=3, 42local output→ (empty) = ""mapped ← 2, output ← 2
pass 1 of 34for index1, value1 in ipairs({1, 2, 3}) do5 local mapped→ 2 = value1 * factor26 if index > 1 then7 output = output .. ","8 end9 output→ 2 = output .. mapped210endAll 3 passes — pass 1 is the card above pass indexvaluemappedoutput1 1 1 2 (empty) → 2 2 2 2 4 2 → 2, 3 3 3 6 2,4 → 2,4, output ← 2,
pass 1 of 25local mapped = value * factor6if index2 > 1 then7 output→ 2, = output .. ","8endoutput ← 2,4
8 end9 output→ 2,4 = output .. mapped410endoutput ← 2,4,
pass 2 of 25local mapped = value * factor6if index3 > 1 then7 output→ 2,4, = output .. ","8endoutput ← 2,4,6
8 end9 output→ 2,4,6 = output .. mapped610endprint("factor=" .. factor)
12print("factor=" .. factor2)13print("output=" .. output2,4,6)outputfactor=2 output=2,4,6
factor ← 3, output ← (empty)
1local factor→ 3 = 32local output→ (empty) = ""mapped ← 3, output ← 3
pass 1 of 34for index1, value1 in ipairs({1, 2, 3}) do5 local mapped→ 3 = value1 * factor36 if index > 1 then7 output = output .. ","8 end9 output→ 3 = output .. mapped310endAll 3 passes — pass 1 is the card above pass indexvaluemappedoutput1 1 1 3 (empty) → 3 2 2 2 6 3 → 3, 3 3 3 9 3,6 → 3,6, output ← 3,
pass 1 of 25local mapped = value * factor6if index2 > 1 then7 output→ 3, = output .. ","8endoutput ← 3,6
8 end9 output→ 3,6 = output .. mapped610endoutput ← 3,6,
pass 2 of 25local mapped = value * factor6if index3 > 1 then7 output→ 3,6, = output .. ","8endoutput ← 3,6,9
8 end9 output→ 3,6,9 = output .. mapped910endprint("factor=" .. factor)
12print("factor=" .. factor3)13print("output=" .. output3,6,9)outputfactor=3 output=3,6,9
factor ← 4, output ← (empty)
1local factor→ 4 = 42local output→ (empty) = ""mapped ← 4, output ← 4
pass 1 of 34for index1, value1 in ipairs({1, 2, 3}) do5 local mapped→ 4 = value1 * factor46 if index > 1 then7 output = output .. ","8 end9 output→ 4 = output .. mapped410endAll 3 passes — pass 1 is the card above pass indexvaluemappedoutput1 1 1 4 (empty) → 4 2 2 2 8 4 → 4, 3 3 3 12 4,8 → 4,8, output ← 4,
pass 1 of 25local mapped = value * factor6if index2 > 1 then7 output→ 4, = output .. ","8endoutput ← 4,8
8 end9 output→ 4,8 = output .. mapped810endoutput ← 4,8,
pass 2 of 25local mapped = value * factor6if index3 > 1 then7 output→ 4,8, = output .. ","8endoutput ← 4,8,12
8 end9 output→ 4,8,12 = output .. mapped1210endprint("factor=" .. factor)
12print("factor=" .. factor4)13print("output=" .. output4,8,12)outputfactor=4 output=4,8,12