0Pricing
Lua Academy · Lesson

Transforming Tables: map, filter, reduce

Build functional higher-order helpers for table transformation.

Transforming Tables: map, filter, reduce is a free Lua Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Functional Patterns in Lua

Lua has no built-in map/filter/reduce, but they are trivial to implement using closures and generic for loops.

map: Transform Every Element

map applies a function to each element and returns a new table of results.

local function map(t, fn)
  local result = {}
  for i, v in ipairs(t) do result[i] = fn(v) end
  return result
end
local doubled = map({1,2,3}, function(x) return x * 2 end)
print(table.concat(doubled, ", "))  -- 2, 4, 6

filter: Keep Matching Elements

filter keeps only elements for which the predicate returns true.

local function filter(t, pred)
  local result = {}
  for _, v in ipairs(t) do
    if pred(v) then result[#result+1] = v end
  end
  return result
end
local evens = filter({1,2,3,4,5}, function(x) return x % 2 == 0 end)
print(table.concat(evens, ", "))  -- 2, 4

reduce: Fold to a Single Value

reduce combines elements left-to-right using an accumulator.

local function reduce(t, fn, init)
  local acc = init
  for _, v in ipairs(t) do acc = fn(acc, v) end
  return acc
end
local sum = reduce({1,2,3,4,5}, function(a,b) return a+b end, 0)
print(sum)  -- 15

Chaining Operations

Chain map, filter, and reduce for expressive data pipelines.

local nums = {1,2,3,4,5,6,7,8,9,10}
local result = reduce(
  map(
    filter(nums, function(x) return x % 2 == 0 end),
    function(x) return x * x end
  ),
  function(a,b) return a + b end, 0
)
print(result)  -- 220

forEach: Side Effects

A forEach iterates without building a new table — useful for side effects like printing.

local function forEach(t, fn)
  for i, v in ipairs(t) do fn(v, i) end
end
forEach({"a","b","c"}, function(v, i) print(i, v) end)

flatMap: Map then Flatten

flatMap maps each element to a list, then concatenates all lists.

local function flatMap(t, fn)
  local result = {}
  for _, v in ipairs(t) do
    for _, item in ipairs(fn(v)) do
      result[#result+1] = item
    end
  end
  return result
end
local expanded = flatMap({1,2,3}, function(x) return {x, x*10} end)

zip: Combine Two Tables

zip pairs elements from two tables into a table of pairs.

local function zip(a, b)
  local result = {}
  for i = 1, math.min(#a, #b) do
    result[i] = {a[i], b[i]}
  end
  return result
end
local zipped = zip({1,2,3}, {"a","b","c"})

Working with Dictionary Tables

For key-value tables, iterate with pairs instead of ipairs and collect key-value pairs.

local function mapValues(t, fn)
  local result = {}
  for k, v in pairs(t) do result[k] = fn(v) end
  return result
end
local doubled = mapValues({x=1,y=2}, function(v) return v*2 end)

Lazy Evaluation Concept

For large datasets, consider lazy iterators (closures that yield one element at a time) to avoid building large intermediate tables.

Performance Note

Each map/filter creates a new table allocation. For tight loops, in-place mutation may be more efficient, but sacrifices immutability and clarity.

reduce Question

What does reduce({1,2,3,4}, function(a,b) return a+b end, 0) return?

Recap: map/filter/reduce

map transforms, filter selects, reduce folds. These three higher-order functions form the foundation of functional data processing in Lua.

Frequently asked questions

Is the “Transforming Tables: map, filter, reduce” lesson free?

Yes — the full text of “Transforming Tables: map, filter, reduce” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.

What will I learn in “Transforming Tables: map, filter, reduce”?

Build functional higher-order helpers for table transformation. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Transforming Tables: map, filter, reduce” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. table.sort with Comparators
  2. table.concat and table.move
  3. Deep Copy and Shallow Copy
  4. Transforming Tables: map, filter, reduce
← Back to Lua Academy