Lamdba function in Lua?

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?

1 Like

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)
1 Like

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 :slight_smile:)

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()
1 Like

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. :upside_down_face:

1 Like

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"?

12 Likes

That was just an example
 :sweat_smile:

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.

4 Likes

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

5 Likes

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 :smile:. (Btw does Roblox Lua use ‘truthy’ for true values and ‘falsey’ for false values?)

1 Like

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.

1 Like

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.

4 Likes

For some context, functions in Lua are considered first-class values.

[Source]


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.

11 Likes

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.

2 Likes

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

2 Likes

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
)()