Transform each value in a table into a new value.

map values Mapping applies the same transformation to every element.

Map Numbers

factor
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)
  1. factor ← 2, output ← (empty)

    1local factor→ 2 = 2 --@factor=3, 42local output→ (empty) = ""
  2. mapped ← 2, output ← 2

    pass 1 of 3
    4for index1, value1 in ipairs({1, 2, 3}) do5  local mapped→ 2 = value1 * factor26  if index > 1 then7    output = output .. ","8  end9  output→ 2 = output .. mapped210end
    All 3 passes — pass 1 is the card above
    passindexvaluemappedoutput
    1112(empty) 2
    22242 2,
    33362,4 2,4,
  3. output ← 2,

    pass 1 of 2
    5local mapped = value * factor6if index2 > 1 then7  output→ 2, = output .. ","8end
  4. output ← 2,4

    8  end9  output→ 2,4 = output .. mapped410end
  5. output ← 2,4,

    pass 2 of 2
    5local mapped = value * factor6if index3 > 1 then7  output→ 2,4, = output .. ","8end
  6. output ← 2,4,6

    8  end9  output→ 2,4,6 = output .. mapped610end
  7. print("factor=" .. factor)

    12print("factor=" .. factor2)13print("output=" .. output2,4,6)
    outputfactor=2
    output=2,4,6
  1. factor ← 3, output ← (empty)

    1local factor→ 3 = 32local output→ (empty) = ""
  2. mapped ← 3, output ← 3

    pass 1 of 3
    4for index1, value1 in ipairs({1, 2, 3}) do5  local mapped→ 3 = value1 * factor36  if index > 1 then7    output = output .. ","8  end9  output→ 3 = output .. mapped310end
    All 3 passes — pass 1 is the card above
    passindexvaluemappedoutput
    1113(empty) 3
    22263 3,
    33393,6 3,6,
  3. output ← 3,

    pass 1 of 2
    5local mapped = value * factor6if index2 > 1 then7  output→ 3, = output .. ","8end
  4. output ← 3,6

    8  end9  output→ 3,6 = output .. mapped610end
  5. output ← 3,6,

    pass 2 of 2
    5local mapped = value * factor6if index3 > 1 then7  output→ 3,6, = output .. ","8end
  6. output ← 3,6,9

    8  end9  output→ 3,6,9 = output .. mapped910end
  7. print("factor=" .. factor)

    12print("factor=" .. factor3)13print("output=" .. output3,6,9)
    outputfactor=3
    output=3,6,9
  1. factor ← 4, output ← (empty)

    1local factor→ 4 = 42local output→ (empty) = ""
  2. mapped ← 4, output ← 4

    pass 1 of 3
    4for index1, value1 in ipairs({1, 2, 3}) do5  local mapped→ 4 = value1 * factor46  if index > 1 then7    output = output .. ","8  end9  output→ 4 = output .. mapped410end
    All 3 passes — pass 1 is the card above
    passindexvaluemappedoutput
    1114(empty) 4
    22284 4,
    333124,8 4,8,
  3. output ← 4,

    pass 1 of 2
    5local mapped = value * factor6if index2 > 1 then7  output→ 4, = output .. ","8end
  4. output ← 4,8

    8  end9  output→ 4,8 = output .. mapped810end
  5. output ← 4,8,

    pass 2 of 2
    5local mapped = value * factor6if index3 > 1 then7  output→ 4,8, = output .. ","8end
  6. output ← 4,8,12

    8  end9  output→ 4,8,12 = output .. mapped1210end
  7. print("factor=" .. factor)

    12print("factor=" .. factor4)13print("output=" .. output4,8,12)
    outputfactor=4
    output=4,8,12