Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous vs Non Anonymous functions Lua

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?

like image 990
Ank Avatar asked Nov 15 '11 19:11

Ank


People also ask

What is an anonymous function Lua?

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.

What is the difference between an anonymous and named function?

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.

Why would you use an anonymous function?

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.

What defines an anonymous function?

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.


2 Answers

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.

like image 184
Michal Kottman Avatar answered Nov 12 '22 07:11

Michal Kottman


The second example is equivalent to
add = 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.

like image 37
Max E. Avatar answered Nov 12 '22 05:11

Max E.