Ank
Ank

Reputation: 6270

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?

Upvotes: 13

Views: 14840

Answers (2)

Max E.
Max E.

Reputation: 1917

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.

Upvotes: 5

Michal Kottman
Michal Kottman

Reputation: 16753

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.

Upvotes: 30

Related Questions