Lua Academy · Lesson

Composing Functions

Chain transformations.

Lesson 4 of 413 steps

Composing Functions is a free Lua Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

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.

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “Composing Functions” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “Composing Functions”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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

  1. Functions as Values
  2. map, filter, reduce
  3. Partial Application
  4. Composing Functions
← Back to Lua Academy