I am learning Lua and have come across the concept of anonymous functions. It's interesting but I was wondering what additional advantage it provides over non anonymous functions.
So If I have something like
function(a,b) return (a+b) end
The function is anonymous and if I have
function add(a,b) return (a+b) end
The function is non anonymous. The second is better because I can call it wherever I want and I also know what my function is doing. So what's the advantage of anonymous functions? Am I missing something here?
Lua Functions Anonymous functions Anonymous functions are just like regular Lua functions, except they do not have a name. doThrice(function() print("Hello!") end) As you can see, the function is not assigned to any name like print or add . To create an anonymous function, all you have to do is omit the name.
Anonymous functions are never hoisted (loaded into memory at compilation). Named functions are hoisted (loaded into memory at compilation). When invoking an anonymous function, you can only call it after the declaration line. A name function can be invoked before declaration.
The advantage of an anonymous function is that it does not have to be stored in a separate file. This can greatly simplify programs, as often calculations are very simple and the use of anonymous functions reduces the number of code files necessary for a program.
An anonymous function is a function that was declared without any named identifier to refer to it. As such, an anonymous function is usually not accessible after its initial creation.
To be honest, there is no such thing as a named function in Lua. All functions are actually anonymous, but can be stored in variables (which have a name).
The named function syntax function add(a,b) return a+b end
is actually a syntactic sugar for add = function(a,b) return a+b end
.
Functions are often used as event handlers and for decisions which a library does not/cannot know, the most famous example being table.sort()
- using your function, you can specify the sorting order:
people = {{name="John", age=20}, {name="Ann", age=25}}
table.sort(people, function (a,b) return a.name < b.name end)
The point is that most probably you won't need the function later. Of course, you can also save the function to a (possibly local) variable and use that:
local nameComparator = function (a,b) return a.name < b.name end
table.sort(people, nameComparator)
For more information, read this section on functions in PiL.
The second example is equivalent toadd = function(a,b) return a+b end
So really you're using anonymous functions all the time, in a trivial sense.
But anonymous functions can get much more useful in other contexts. For example, using functions to mutate other functions (the soul of functional programming.)
function make_version_with_n_args (func, n)
if n == 1 then
return function (x) return x end
else
return function (x, ...)
return func (x, make_version_with_n_args (func, n-1)(...))
end
end
end
add_four = make_version_with_n_args (function (a, b) return a+b end, 4)
print (add_four(3, 3, 3, 3))
add_sizes = {}
for i = 1, 5 do
add_sizes[i] = make_version_with_n_args(function (a, b) return a+b end, i)
end
func_args = {}
for i = 1, 5 do
func_args[#func_args+1] = 2
print (add_sizes[i](unpack(func_args)))
end
function make_general_version (func)
return function (...)
local args = {...}
local result = args[#args]
for i = #args-1,1,-1 do
result = func(args[i], result)
end
return result
end
end
general_add = make_general_version (function (a, b) return a+b end)
print (general_add(4, 4, 4, 4))
Basically, you can create a name for every single function if you want to, but in situations where you are throwing around so many one-off functions, it is more convenient not to do so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With