Partial Application
Pre-fill function arguments.
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))All lessons in this course
- Functions as Values
- map, filter, reduce
- Partial Application
- Composing Functions