Page 1 of 1

Lambda functions

Posted: Sun Jul 23, 2023 3:23 am
by DaveTheCoder
Lambda functions are cool. (New feature in Godot 4)

Connecting a signal:

Code: Select all

$VisibilityNotifier2D.screen_exited.connect(func(): queue_free())
Connecting a signal with a parameter:

Code: Select all

Events.volume_changed.connect(func(v: float): volume_db = v)
Deferring a signal emission:

Code: Select all

(func(): value_changed.emit(value)).call_deferred()
https://docs.godotengine.org/en/4.1/tut ... -functions

Re: Lambda functions

Posted: Sun Jul 23, 2023 8:46 pm
by grey
maybe i should get in the habit of writing one-liners as lambdas

what does deferring a signal emission do ? does it emit the signal at the very end of the frame or something like that ?

edit : i think that's it (source : https://docs.godotengine.org/en/4.1/cla ... umerations)

Re: Lambda functions

Posted: Sun Jul 23, 2023 11:30 pm
by DaveTheCoder
what does deferring a signal emission do ? does it emit the signal at the very end of the frame or something like that?
Yes. I'm doing that when emitting a signal in a node's _ready() method, since the nodes that receive the signal may not be ready at that time.

Re: Lambda functions

Posted: Fri Jul 28, 2023 10:31 pm
by Owen Lloyd
How would you define a lambda function without using the words/term "lambda function".

I struggle to grasp the concept and the value, This is a reflection of my limited brain capacity, not of the concept, lol. But I don't get it, it seems to me to be taking python readability and making it C++ readability and I cannot work out the advantage.

Re: Lambda functions

Posted: Fri Jul 28, 2023 10:50 pm
by DaveTheCoder
I think the first couple of paragraphs of this article answer your question. The "lambda" comes from the historical origin. "Anonymous function" might be a better term.
In computer programming, an anonymous function (function literal, lambda abstraction, lambda function, lambda expression or block) is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function.[1] If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.

Anonymous functions originate in the work of Alonzo Church in his invention of the lambda calculus, in which all functions are anonymous, in 1936, before electronic computers.[2] In several programming languages, anonymous functions are introduced using the keyword lambda, and anonymous functions are often referred to as lambdas or lambda abstractions. Anonymous functions have been a feature of programming languages since Lisp in 1958, and a growing number of modern programming languages support anonymous functions.
https://en.wikipedia.org/wiki/Lambda_fu ... ogramming)

Re: Lambda functions

Posted: Sat Jul 29, 2023 12:02 am
by Owen Lloyd
Hi thanks. :)

I will pretend I understand until I do.

Re: Lambda functions

Posted: Sat Jul 29, 2023 12:34 am
by DaveTheCoder
I think it's hard to understand new programming constructs by merely reading about them. You have to use them in your own code and experiment.

Re: Lambda functions

Posted: Sat Jul 29, 2023 1:32 am
by Owen Lloyd
Okay, I think I get it.

I am still not sure why they are advantageous (outside of typing less). Speedier execution maybe? Less memory used? Easier for project maintenance?

Not a biggie, thanks for you feedback. :)

Re: Lambda functions

Posted: Sat Jul 29, 2023 2:08 am
by DaveTheCoder
In my opinion, it can make the code more compact and easier to read.

But there's a risk of making it harder to read. Less code is not always better.

It also gives you bragging rights:
I'm superior to you because I know how to use this feature and you don't. :P

Re: Lambda functions

Posted: Sat Jul 29, 2023 5:38 am
by Owen Lloyd
lol :lol:

Code: Select all

var call_out = func(): print('It must be true')
Thanks for the explanations.