0Pricing
Lua Academy · Lesson

Partial Application

Pre-fill function arguments.

Partial Application is a free Lua Academy lesson on CoddyKit — lesson 3 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 Partial Application?

Partial application means fixing some of a function's arguments now and supplying the rest later. You get back a new function that needs fewer arguments.

This lets you turn a general function into a specialized one by locking in the values you already know.

A Manual Example

Suppose you have an add function. You can build addFive by writing a closure that captures 5 and waits for the second number.

The captured value lives in the returned function, so each call only needs the remaining argument.

local function add(a, b)
  return a + b
end

local function addFive(b)
  return add(5, b)
end

print(addFive(10))
print(addFive(2))

Binding the First Argument

We can generalize that pattern. A bind1 helper takes a function and a fixed first argument, and returns a new function expecting the second.

The closure captures both f and the locked value, then forwards everything when called.

local function bind1(f, a)
  return function(b)
    return f(a, b)
  end
end

local function mul(x, y) return x * y end
local double = bind1(mul, 2)
print(double(7))
print(double(50))

Using Varargs

To fix any number of leading arguments, use Lua's vararg syntax .... You capture the fixed arguments, then append the later ones at call time.

The table.pack and table.unpack functions help store and spread argument lists.

local function partial(f, ...)
  local fixed = table.pack(...)
  return function(...)
    local rest = table.pack(...)
    local args = {}
    for i = 1, fixed.n do args[i] = fixed[i] end
    for i = 1, rest.n do args[fixed.n + i] = rest[i] end
    return f(table.unpack(args, 1, fixed.n + rest.n))
  end
end

local function add3(a, b, c) return a + b + c end
local add10 = partial(add3, 10)
print(add10(2, 3))

Fixing Several Arguments

Because partial accepts a vararg, you can lock in more than one argument at once. The returned function then needs only the remaining ones.

Here two of three arguments are pre-filled, leaving a single-argument function.

local function partial(f, ...)
  local fixed = table.pack(...)
  return function(...)
    local rest = table.pack(...)
    local args = {}
    for i = 1, fixed.n do args[i] = fixed[i] end
    for i = 1, rest.n do args[fixed.n + i] = rest[i] end
    return f(table.unpack(args, 1, fixed.n + rest.n))
  end
end

local function label(prefix, sep, value) return prefix .. sep .. value end
local tag = partial(label, "id", ":")
print(tag(42))

A Practical Use

Partial application shines with general utility functions. Fix the configuration once, then reuse the specialized function many times.

Below, a generic greet becomes a fixed-language greeter that only needs a name.

local function greet(lang, name)
  if lang == "tr" then return "Merhaba, " .. name end
  return "Hello, " .. name
end

local function bind1(f, a) return function(b) return f(a, b) end end
local greetTr = bind1(greet, "tr")
print(greetTr("Ada"))
print(greetTr("Lin"))

Partial vs Currying

Currying turns a multi-argument function into a chain of single-argument functions, each returning the next. Partial application just fixes some arguments and keeps the rest as a normal call.

They are related but distinct: currying is one-at-a-time, partial is fix-some-now.

local function curryAdd(a)
  return function(b)
    return function(c)
      return a + b + c
    end
  end
end

print(curryAdd(1)(2)(3))

Currying a Two-Arg Function

A small helper can curry any two-argument function automatically. It returns a function of the first argument that returns a function of the second.

This produces reusable, single-argument builders that read nicely in pipelines.

local function curry2(f)
  return function(a)
    return function(b)
      return f(a, b)
    end
  end
end

local function pow(base, exp) return base ^ exp end
local cpow = curry2(pow)
print(cpow(2)(10))

Closures Make It Work

Every partial and curried function relies on closures. The fixed arguments are captured locals that the returned function remembers.

Because each returned function has its own captured values, you can create many specialized variants safely.

local function bind1(f, a) return function(b) return f(a, b) end end
local function sub(x, y) return x - y end

local from100 = bind1(sub, 100)
local from10 = bind1(sub, 10)
print(from100(40))
print(from10(3))

When to Reach for It

Use partial application to remove repetition when you call a function with the same leading arguments over and over.

It also helps adapt a function to an interface that expects fewer arguments, such as a callback that passes only one value.

Configured Callbacks

Partial application pairs well with map and filter. You can pre-fill a configuration argument so the callback receives only the list element.

This keeps your pipeline clean while still letting you tune behavior.

local function map(t, f) local o={} for i,v in ipairs(t) do o[i]=f(v) end return o end
local function bind1(f, a) return function(b) return f(a, b) end end
local function scale(factor, x) return factor * x end

local r = map({1,2,3,4}, bind1(scale, 10))
print(table.concat(r, ", "))

Quick Check

Recall the difference between two related ideas.

Recap

Partial application fixes some arguments and returns a new function expecting the rest, built with closures that capture the fixed values.

A vararg-based partial helper generalizes this, while currying chains single-argument functions. Both reduce repetition and adapt functions to callbacks.

Frequently asked questions

Is the “Partial Application” lesson free?

Yes — the full text of “Partial Application” 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 “Partial Application”?

Pre-fill function arguments. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Partial Application” 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