The title says it all: what is the equivalent of a lambda function (anonymous function) in Lua? lambda
is not a keyword in Lua, what should I use instead?
Why would you need λ ? for v=fλ to go get the wavelength of a wave or what I donât understand.
Edit from 1 year in the future: some experience in Python now, the answer to this question would be that the shortest you can go in Lua and closest to a lambda (without assignment here but you can do that too) is:
(function()print(0)end)()
what would be the equivalent of this in Python:
(lambda: print(0))()
and the rough equivalent in Javascript for good measure:
(v => console.log(v))(0)
Oh no no, you misunderstood me. The lamdba I was talking about was the lambda for anonymous functions. (Iâm used to the programming language Python, and it uses the keyword lamdba
for anonymous functions. Sorry for the Python lingo )
Iâm unfamiliar with Python. Can you give an example/criteria for anonymous functions?
What iâm trying to do is to create a function without a name then instantaneously run it. I want it to return a value and then assign that value to a variable, all in one line. Is it possible?
Kind of like this:
local var = function() return "somevalue" end
The code above assigns var
to the function. What I want is to assign var
to the value "somevalue"
.
There is no special syntax in Roblox Lua for abbreviating anonymous function creation. The example you gave using the function keyword is the correct way to do this.
local lambda = function () return "value" end
local var = lambda()
I am using many âlambdasâ in my script. I have thought of that method before, but I donât want my script to be too messy. But if that is the only way to do it, so be it.
Itâs possible to shorten it without needing to assign a lambda expression to a variable, simply wrap it in parentheses.
local var = (function() return "somevalue" end)()
But I have to ask: Why do you want to do this? This is usually a sign of code smell. Why not just local var = "somevalue"
?
That was just an exampleâŠ
I want the function to run an if
statement. If the condition applies, it will return a value. If not, it will return some other value.
Thanks for your answer anyways, I didnât know you could do that! (Sometimes you have to âthink outside the boxâ)
Function literals are a thing. You can pass a function as an argument to another function without any variable being associated with it first, e.g.
local function do_function(f)
f()
end
do_function(function() print("hello world") end)
Function literals are really the only thing. Variables just contain them sometimes.
ternary exists in roblox. These are easily done like so:
x == 2
y == x
local variable = (x == y) and "X equals to Y" or "X does not equal to Y"
print(Variable)
this full on works like a return if statement. this line basically stands for
local value;
value = (function()
if x == y then
return "X equals to Y"
else
return "X does not equal to Y"
end
end)()
I use this all the time for my code, itâs basically a one liner for returning values to a variable
Oh wow, this âternaryâ thing helps!
Iâve experimented around a bit, and
and or
works differently in Lua than in Python. In Python, "a" and "b"
would return true, but in Lua it would return "b"
. And or
too, "a" or "b"
in Lua returns âaâ, while in Python it would return false.
So I figured it out. With and
, the first and second value is a truthy value, it would return the second. But if one of them is a truthy and the other is a falsey, it would return the falsey. And if both of them are falseys, it would return the first. or
is basicly the opposite of and
. When it gets two truthies, it would return the first. Two falseys = second value. One falsey and truthy and it will always return the truthy value.
I need to start to become familiar to Lua . (Btw does Roblox Lua use âtruthyâ for true values and âfalseyâ for false values?)
I donât think lua makes use of truthy or falsey. But that is just a hunch i wouldnât know however i never heard of it in the lua aspect. You could always check for the value being null as a in between state.
cc @VoidedBIade
Yes, Lua does use truthy and falsy, itâs just a way of conveying things and I doubt itâs a language specific concept. Theyâd mean exactly what youâd think. For example, in an if statement without explicit checking for false or nil, both are falsy values.
For some context, functions in Lua are considered first-class values.
Small tangent on Lua ternary operators:
Lua does not have a ternary operator. Using the âand/orâ logic is a pseudo-ternary operation, and it fails in certain contexts, so itâs really important to know why it acts similar to a ternary operator.
Put simply, Lua expressions will resolve to the last value looked at.
a and b or c
will resolve to b
if a
is truthy, but b
also has to be truthy. This is where the pseudo-ternary operator in Lua fails. If b
is falsey, then it will move on to the or
condition and resolve to c
regardless of its value.
So, if you ever need to resolve to a falsey value under the first a
condition, then you need to use a traditional if
statement.
This same thing is true in languages like JavaScript. You could do a && b || c
, but it would suffer from the same issues. JS has a dedicated ternary operator to fix this: a ? b : c
. Unfortunately, Lua does not.
Not knowing this tripped me up a lot for quite a while, so just wanted to clear up whatâs happening. I use the pseudo-ternary operation in Lua all the time! You just need to know when it can be used.
Not to be contradictory, but thatâs really not how and/or work in Python. It works the same there as far as I can tell.
Yeah Python has the same sort of thing going on like Lua. Thereâs a shorthand if
in Python though that they call conditional expressions: a if b else c
There is a pretty big difference between lambda expressions and anonymous functions in some languages. For example Matlab has subfunctions and lambda expressions. The lambda expressions create a copy of the upvalues when they are created, but the subfunctions share the scope of their parent and can access the most recent version of upvalues.
The idea of lambda expressions comes from the lambda calculus, a purely functional language. In functional languages there are no such things as statements. Thus, entire programs are a single large expression. There are also no âassignmentâ statements and no storage. This carries over to lambda expressions which can only be expressions, unlike functions in procedural languages. (It is unfortunate that âfunctionsâ have become synonymous with âproceduresâ. The âfunctionsâ we know in most languages including Lua should be called âproceduresâ to better distinguish them from what are now known as âpure functionsâ, the original meaning of a function without state like in mathematics.) Lambda expressions cannot edit variables or access upvalues. Upvalues are copied as constants when a lambda expression is created.
Anonymous functions are what we have in Lua. These can contain many statements and are not pure functions like in the lambda calculus. They are more dynamic.
I should temper my comments by saying that the differences between lambda expressions, lambda functions, anonymous functions, subfunctions, first class functions, procedures, and weâll throw methods into the mix, have become blurred and really depends on which language you are using. Some languages have large differences while others is just syntax. Iâve tried to stay true to the original meaning in my comments.
Wait, really? After one year of working with python and I still donât knowâŠ
The lambda expression that he means is like that:
Normal function
(function()
â TODO
end)()
Lambda function
(() =>
â TODO
)()