Combine mapping, filtering, and reducing in one pass.

pipeline result A processing pipeline can transform values, filter them, and reduce the kept values to one result.

Pipeline Result

bonus
pipeline_result.lua
Replay: real traced execution (multi-file project)
local bonus = 1
local total = 0

for _, value in ipairs({1, 2, 3}) do
  local mapped = value + bonus
  if mapped % 2 == 0 then
    total = total + mapped
  end
end

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

for _, value in ipairs({1, 2, 3}) do
  local mapped = value + bonus
  if mapped % 2 == 0 then
    total = total + mapped
  end
end

print("bonus=" .. bonus)
print("total=" .. total)
local bonus = 3
local total = 0

for _, value in ipairs({1, 2, 3}) do
  local mapped = value + bonus
  if mapped % 2 == 0 then
    total = total + mapped
  end
end

print("bonus=" .. bonus)
print("total=" .. total)
  1. bonus ← 1, total ← 0

    1local bonus→ 1 = 1 --@bonus=0, 32local total→ 0 = 0
  2. mapped ← 2

    pass 1 of 3
    4for _1, value1 in ipairs({1, 2, 3}) do5  local mapped→ 2 = value1 + bonus16  if mapped % 2 == 0 then
    All 3 passes — pass 1 is the card above
    pass_valuemappedtotal
    11120 2
    2223
    33342 6
  3. total ← 2

    pass 1 of 2
    5local mapped = value + bonus6if mapped2 % 2 == 0 then7  total→ 2 = total + mapped28end
  4. total ← 6

    pass 2 of 2
    5local mapped = value + bonus6if mapped4 % 2 == 0 then7  total→ 6 = total + mapped48end
  5. print("bonus=" .. bonus)

    11print("bonus=" .. bonus1)12print("total=" .. total6)
    outputbonus=1
    total=6
  1. bonus ← 0, total ← 0

    1local bonus→ 0 = 02local total→ 0 = 0
  2. mapped ← 1

    pass 1 of 3
    4for _1, value1 in ipairs({1, 2, 3}) do5  local mapped→ 1 = value1 + bonus06  if mapped % 2 == 0 then
    All 3 passes — pass 1 is the card above
    pass_valuemappedtotal
    1111
    22220 2
    3333
  3. total ← 2

    5local mapped = value + bonus6if mapped2 % 2 == 0 then7  total→ 2 = total + mapped28end
  4. print("bonus=" .. bonus)

    11print("bonus=" .. bonus0)12print("total=" .. total2)
    outputbonus=0
    total=2
  1. bonus ← 3, total ← 0

    1local bonus→ 3 = 32local total→ 0 = 0
  2. mapped ← 4

    pass 1 of 3
    4for _1, value1 in ipairs({1, 2, 3}) do5  local mapped→ 4 = value1 + bonus36  if mapped % 2 == 0 then
    All 3 passes — pass 1 is the card above
    pass_valuemappedtotal
    11140 4
    2225
    33364 10
  3. total ← 4

    pass 1 of 2
    5local mapped = value + bonus6if mapped4 % 2 == 0 then7  total→ 4 = total + mapped48end
  4. total ← 10

    pass 2 of 2
    5local mapped = value + bonus6if mapped6 % 2 == 0 then7  total→ 10 = total + mapped68end
  5. print("bonus=" .. bonus)

    11print("bonus=" .. bonus3)12print("total=" .. total10)
    outputbonus=3
    total=10