Functional Table Processing
Filter Values
Keep only values that pass a condition.
filter values
Filtering visits every element but only records the values that match the predicate.
Filter Values
filter_values.lua
Replay: real traced execution (multi-file project)
local limit = 4
local output = ""
local count = 0
for _, value in ipairs({2, 4, 6, 8}) do
if value >= limit then
count = count + 1
if output ~= "" then
output = output .. ","
end
output = output .. value
end
end
print("limit=" .. limit)
print("count=" .. count)
print("output=" .. output)
local limit = 3
local output = ""
local count = 0
for _, value in ipairs({2, 4, 6, 8}) do
if value >= limit then
count = count + 1
if output ~= "" then
output = output .. ","
end
output = output .. value
end
end
print("limit=" .. limit)
print("count=" .. count)
print("output=" .. output)
local limit = 6
local output = ""
local count = 0
for _, value in ipairs({2, 4, 6, 8}) do
if value >= limit then
count = count + 1
if output ~= "" then
output = output .. ","
end
output = output .. value
end
end
print("limit=" .. limit)
print("count=" .. count)
print("output=" .. output)
limit ← 4, output ← (empty), count ← 0
1local limit→ 4 = 4 --@limit=3, 62local output→ (empty) = ""3local count→ 0 = 0_, value in ipairs({2, 4, 6, 8}) do
pass 1 of 45for _1, value2 in ipairs({2, 4, 6, 8}) do6 if value >= limit then7 count = count + 1All 4 passes — pass 1 is the card above pass _valueoutput1 1 2 — 2 2 4 — 3 3 6 4 → 4, 4 4 8 4,6 → 4,6, count ← 1, output ← 4
pass 1 of 35for _, value in ipairs({2, 4, 6, 8}) do6 if value4 >= limit4 then7 count→ 1 = count + 18 if output ~= "" then9 output = output .. ","10 end11 output→ 4 = output .. value412 endAll 3 passes — pass 1 is the card above pass valuecountoutput1 4 0 → 1 (empty) → 4 2 6 1 → 2 4 → 4, 3 8 2 → 3 4,6 → 4,6, output ← 4,
pass 1 of 27count = count + 18if output4 ~= "" then9 output→ 4, = output .. ","10endoutput ← 4,6
10 end11 output→ 4,6 = output .. value612endoutput ← 4,6,
pass 2 of 27count = count + 18if output4,6 ~= "" then9 output→ 4,6, = output .. ","10endoutput ← 4,6,8
10 end11 output→ 4,6,8 = output .. value812endprint("limit=" .. limit)
15print("limit=" .. limit4)16print("count=" .. count3)17print("output=" .. output4,6,8)outputlimit=4 count=3 output=4,6,8
limit ← 3, output ← (empty), count ← 0
1local limit→ 3 = 32local output→ (empty) = ""3local count→ 0 = 0_, value in ipairs({2, 4, 6, 8}) do
pass 1 of 45for _1, value2 in ipairs({2, 4, 6, 8}) do6 if value >= limit then7 count = count + 1All 4 passes — pass 1 is the card above pass _valueoutput1 1 2 — 2 2 4 — 3 3 6 4 → 4, 4 4 8 4,6 → 4,6, count ← 1, output ← 4
pass 1 of 35for _, value in ipairs({2, 4, 6, 8}) do6 if value4 >= limit3 then7 count→ 1 = count + 18 if output ~= "" then9 output = output .. ","10 end11 output→ 4 = output .. value412 endAll 3 passes — pass 1 is the card above pass valuecountoutput1 4 0 → 1 (empty) → 4 2 6 1 → 2 4 → 4, 3 8 2 → 3 4,6 → 4,6, output ← 4,
pass 1 of 27count = count + 18if output4 ~= "" then9 output→ 4, = output .. ","10endoutput ← 4,6
10 end11 output→ 4,6 = output .. value612endoutput ← 4,6,
pass 2 of 27count = count + 18if output4,6 ~= "" then9 output→ 4,6, = output .. ","10endoutput ← 4,6,8
10 end11 output→ 4,6,8 = output .. value812endprint("limit=" .. limit)
15print("limit=" .. limit3)16print("count=" .. count3)17print("output=" .. output4,6,8)outputlimit=3 count=3 output=4,6,8
limit ← 6, output ← (empty), count ← 0
1local limit→ 6 = 62local output→ (empty) = ""3local count→ 0 = 0_, value in ipairs({2, 4, 6, 8}) do
pass 1 of 45for _1, value2 in ipairs({2, 4, 6, 8}) do6 if value >= limit then7 count = count + 1All 4 passes — pass 1 is the card above pass _valuelimitcountoutput1 1 2 — — — 2 2 4 — — — 3 3 6 6 0 → 1 (empty) → 6 4 4 8 6 1 → 2 6 → 6, count ← 1, output ← 6
pass 1 of 25for _, value in ipairs({2, 4, 6, 8}) do6 if value6 >= limit6 then7 count→ 1 = count + 18 if output ~= "" then9 output = output .. ","10 end11 output→ 6 = output .. value612 endcount ← 2
pass 2 of 25for _, value in ipairs({2, 4, 6, 8}) do6 if value8 >= limit6 then7 count→ 2 = count + 18 if output ~= "" thenoutput ← 6,
7count = count + 18if output6 ~= "" then9 output→ 6, = output .. ","10endoutput ← 6,8
10 end11 output→ 6,8 = output .. value812endprint("limit=" .. limit)
15print("limit=" .. limit6)16print("count=" .. count2)17print("output=" .. output6,8)outputlimit=6 count=2 output=6,8