Notifications
Clear all

Is this a Lambda function?

8 Posts
3 Users
0 Likes
2,256 Views
(@garnold)
Member
Joined: 5 years ago
Posts: 81
Topic starter  

I'm trying to better understand this piece of code from an example I'm working on. The project is working but I hate when I snag a piece of code from an example, use it, but not really understand it. Does this code have a Lambda function in it? Is that what the [] and the > have to do with? If it is, can I get some help or links to someplace to learn how they work please? Do you folks use them often? Finally, what would this code look like if it was just a normal function? Thank you!

server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readHumi().c_str());
});

I did find this link but not sure if it's what I should be looking at
https://www.cprogramming.com/c++11/c++11-lambda-closures.html

 

And this is the page I got the code snippet from. Not sure if more code context is needed to better answer my question.

https://randomnerdtutorials.com/esp8266-nodemcu-client-server-wi-fi/


   
Quote
Topic Tags
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@garnold

It looks like lambada to me, but I've rarely used it, and not for a long time.  Originally, custom STL libraries such as BOOST offered it, but I've never used it since it was introduced into t he C++ 11 ISO standard.

As for the ">", do you you mean the "->" operator? If so, that is just a pointer to member dereference operator:

  struct Object {
    int X;
   }; 

  // Normal dereferencing syntax
  Object A;
  A.X = 101;

  // Pointer dereferencing syntax
  Object* B = new Object();
  B->X = 101;

Cheers!


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

@garnold

Yes it is using a lambda and, as @frogandtoad explained, the "->" is just used to dereference the "request" pointer and get the "send_P" member from the AsyncWebServerRequest class instance that "request" points to.

Using the intellisense in PlatformIO on similar code built from another Random Nerd Tutorials example :

image

hovering the mouse over the "on" from "oWebServer.on" we have :

image

Using the "Find Declaration" feature we arrive in ESPAsyncWebServer.h at line 418, and the 3rd argument "onRequest" for this overloaded "on" member is of type ArRequestHandlerFunction (which is the typedef at line 387).

I marked both lines with a breakpoint red dot :

image

The ArRequestHandlerFunction type is "function returning void and taking 1 argument : AsyncWebServerRequest *request"

So the original piece of code above, with linenums 176 to 178, is equivalent to this non-lambda version :

void myRequestHandler(AsyncWebServerRequest *request)
{
request->send_P(200, "text/plain", outputTemperature().c_str());
}
oWebServer.on("/temperature", HTTP_GET, myRequestHandler);

 

And similarly your own piece of code could be written :

void yourRequestHandler(AsyncWebServerRequest *request)
{
  request->send_P(200, "text/plain", readHumi().c_str());
}
server.on("/temperature", HTTP_GET, yourRequestHandler);

 

 

Interestingly, building the original (lambda) code and the alternate (non-lambda) gives a slight difference in flash memory :

with lambda :

image

 

with explicit function :

image

 

Eric


   
ReplyQuote
(@garnold)
Member
Joined: 5 years ago
Posts: 81
Topic starter  

Thank you both very much! 

How about the * in the *request section of the code? 

What does the * have to do with things?

I'm guessing this is all C++ stuff correct? Arduino is not a language and uses C++ so if I want to really learn this stuff I need to dig into C++ right?


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 
Posted by: @garnold

I'm guessing this is all C++ stuff correct?

That's right, but actually pointers were already there plain standard C

The "X *a" type of syntax up there denotes a pointer : "a" is a pointer to a structure/object/function/thingie of type "X"

Posted by: @garnold

if I want to really learn this stuff I need to dig into C++ right?

That would help ? 

Eric


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

@garnold

On the other hand, most of microcontroller programming only uses rather basic features of C/C++, plus the specifics of the platform (the API = functions provided by the Arduino framework like digitalRead, etc...) so without being an expert of the C++ language you'll already be able to achieve quite a lot

Eric


   
ReplyQuote
(@garnold)
Member
Joined: 5 years ago
Posts: 81
Topic starter  

@zeferby

Yeah I agree with you on this. I really don't want to become a C++ expert here. I'm afraid I would spend so much time learning C++ that I would never spend any time making cool stuff! Maybe I'll just keep trucking along and ask you smart guys all my questions 😉

Thank you very much!


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@garnold

Posted by: @garnold

Arduino is not a language and uses C++ so if I want to really learn this stuff I need to dig into C++ right?

Correct... Arduino is not a language, but a product... in fact a micro-controller, and you use the C++ language to write programs for it, using an integrated development environment (IDE), such as the Arduino one that most people download from the Arduino website.

Just like any language, there is a bunch of predefined library functionality available for you to use, but you'll still need to understand basic programming principles, such as iteration, conditional statements, etc.

There are many people here that can help, so just ask.

Cheers!


   
ReplyQuote