Functions as Values
Store and pass functions.
Functions Are First-Class
In Lua, functions are first-class values. That means a function is just data: you can store it in a variable, pass it to another function, return it, and keep it in a table.
This single idea powers all of functional programming. Once you treat functions like numbers or strings, you can build flexible, reusable tools.
Storing a Function in a Variable
The name you give a function is really just a variable holding the function value. You can assign that value to another variable freely.
Both names below point to the same underlying function, so calling either one runs the same code.
local function greet(name)
return "Hello, " .. name
end
local hi = greet
print(greet("Ada"))
print(hi("Lin"))All lessons in this course
- Functions as Values
- map, filter, reduce
- Partial Application
- Composing Functions