Combine values into one accumulated result.

reduce values Reducing starts from an accumulator and folds each value into it.

Reduce Total

start
reduce_total.lua
Replay: real traced execution (multi-file project)
local start = 10
local total = start

for _, value in ipairs({1, 2, 3}) do
  total = total + value
end

print("start=" .. start)
print("total=" .. total)
local start = 0
local total = start

for _, value in ipairs({1, 2, 3}) do
  total = total + value
end

print("start=" .. start)
print("total=" .. total)
local start = 100
local total = start

for _, value in ipairs({1, 2, 3}) do
  total = total + value
end

print("start=" .. start)
print("total=" .. total)
  1. start ← 10, total ← 10

    1local start→ 10 = 10 --@start=0, 1002local total→ 10 = start10
  2. total ← 11

    pass 1 of 3
    4for _1, value1 in ipairs({1, 2, 3}) do5  total→ 11 = total + value16end
    All 3 passes — pass 1 is the card above
    pass_valuetotal
    11110 11
    22211 13
    33313 16
  3. print("start=" .. start)

    8print("start=" .. start10)9print("total=" .. total16)
    outputstart=10
    total=16
  1. start ← 0, total ← 0

    1local start→ 0 = 02local total→ 0 = start0
  2. total ← 1

    pass 1 of 3
    4for _1, value1 in ipairs({1, 2, 3}) do5  total→ 1 = total + value16end
    All 3 passes — pass 1 is the card above
    pass_valuetotal
    1110 1
    2221 3
    3333 6
  3. print("start=" .. start)

    8print("start=" .. start0)9print("total=" .. total6)
    outputstart=0
    total=6
  1. start ← 100, total ← 100

    1local start→ 100 = 1002local total→ 100 = start100
  2. total ← 101

    pass 1 of 3
    4for _1, value1 in ipairs({1, 2, 3}) do5  total→ 101 = total + value16end
    All 3 passes — pass 1 is the card above
    pass_valuetotal
    111100 101
    222101 103
    333103 106
  3. print("start=" .. start)

    8print("start=" .. start100)9print("total=" .. total106)
    outputstart=100
    total=106