Composing Functions
Chain transformations.
Composing Functions 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.
What Is Composition?
Function composition combines two functions into one. The composed function feeds its input through the first function, then passes that result to the second.
In math this is written f(g(x)). Composition lets you build big transformations out of small, focused functions.
Composing Two Functions
A compose helper takes two functions and returns a new one. Calling it runs g first, then feeds the result into f.
The order matters: the inner function runs first, the outer function runs on its output.
local function compose(f, g)
return function(x)
return f(g(x))
end
end
local function inc(n) return n + 1 end
local function dbl(n) return n * 2 end
local incThenDouble = compose(dbl, inc)
print(incThenDouble(3))Order Is Important
Swapping the arguments changes the result. compose(f, g) means apply g first, then f.
The example below shows that doubling then incrementing gives a different answer from incrementing then doubling.
local function compose(f, g) return function(x) return f(g(x)) end end
local function inc(n) return n + 1 end
local function dbl(n) return n * 2 end
print(compose(dbl, inc)(3))
print(compose(inc, dbl)(3))Composing String Functions
Composition works with any single-argument functions, including string transformations. Chain trimming, casing, or reversing into one reusable step.
Here reverse runs first, then upper, giving an uppercased reversed string.
local function compose(f, g) return function(x) return f(g(x)) end end
local shout = compose(string.upper, string.reverse)
print(shout("lua"))Composing Many Functions
To chain more than two functions, write a variadic composeAll. It applies the functions from right to left, like nested calls.
The loop walks the function list backward, threading the value through each one.
local function composeAll(...)
local fns = {...}
return function(x)
for i = #fns, 1, -1 do
x = fns[i](x)
end
return x
end
end
local f = composeAll(function(n) return n + 1 end,
function(n) return n * 2 end,
function(n) return n - 3 end)
print(f(10))Right to Left
In composeAll(a, b, c) the rightmost function c runs first, then b, then a. This matches the nested form a(b(c(x))).
Reading the order can be tricky, which is why some people prefer a left-to-right pipe instead.
A Left-to-Right Pipe
A pipe reverses the reading order: functions run left to right, in the order written. Many find this easier to follow for data pipelines.
The loop walks forward, applying each function to the running value.
local function pipe(...)
local fns = {...}
return function(x)
for i = 1, #fns do
x = fns[i](x)
end
return x
end
end
local process = pipe(function(n) return n + 1 end,
function(n) return n * 2 end)
print(process(5))Pipe for Readability
With pipe, the steps read top to bottom in execution order. This makes multi-stage transformations self-documenting.
Here a string is trimmed of a prefix idea, lowercased, then reversed, each step listed in the order it happens.
local function pipe(...) local fns={...} return function(x) for i=1,#fns do x=fns[i](x) end return x end end
local transform = pipe(string.lower, string.reverse,
function(s) return "[" .. s .. "]" end)
print(transform("LUA"))Composition with Closures
You can compose functions built by partial application or closures. Build small specialized functions, then glue them together.
This keeps each piece testable while the composition expresses the overall flow.
local function pipe(...) local fns={...} return function(x) for i=1,#fns do x=fns[i](x) end return x end end
local function adder(n) return function(x) return x + n end end
local function muller(n) return function(x) return x * n end end
local calc = pipe(adder(3), muller(10))
print(calc(2))Composition and Pipelines
Composition is the glue behind data pipelines. Instead of nesting calls deeply, you name a composed function once and apply it to many inputs.
This works beautifully with map: pass a composed function as the per-item transform.
local function map(t, f) local o={} for i,v in ipairs(t) do o[i]=f(v) end return o end
local function compose(f, g) return function(x) return f(g(x)) end end
local step = compose(function(n) return n + 1 end, function(n) return n * n end)
local r = map({1, 2, 3, 4}, step)
print(table.concat(r, ", "))Keep Functions Small
Composition rewards small, single-purpose functions. Each does one job, and you combine them to form complex behavior without rewriting logic.
This is the heart of functional design: build a vocabulary of tiny functions, then compose them to express larger ideas.
Quick Check
Trace the order carefully before answering.
Recap
Composition combines functions so the output of one feeds the next. compose(f, g) runs g then f, right to left, while pipe runs left to right for readability.
With composeAll and pipe you chain many small functions into clear pipelines, especially powerful when paired with map.
Frequently asked questions
Is the “Composing Functions” lesson free?
Yes — the full text of “Composing Functions” 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 “Composing Functions”?
Chain transformations. 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 “Composing Functions” 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
- Functions as Values
- map, filter, reduce
- Partial Application
- Composing Functions