$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS ESP32ESP8266MicroPython

ESP32 / ESP8266 MicroPython Tutorial: Lambda functions

DFRobot Aug 30 2017 908

The objective of this MicroPython Tutorial is to explain how to use lambda functions in MicroPython and their difference regarding regular functions. This tutorial was tested both on the ESP32 and on the ESP8266. The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.

Introduction

The objective of this MicroPython Tutorial is to explain how to use lambda functions in MicroPython and their difference regarding regular functions.

Lambdas are a concept supported in many other programming languages, such as JavaScript and C#, and in a more formal definition they correspond to anonymous functions.

So, in MicroPython, this means that we can define functions at run-time without the typical def structure. Although they have many uses, one particular application of lambdas is their use in functions such as the map, filter and reduce, which are applied to lists.

The tests shown here were performed on both the ESP32 and the ESP8266. The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The images shown through the tutorial are from the tests on the ESP32.

The commands shown bellow were executed on uPyCraft, a MicroPython IDE. You can learn more about it in this previous post.
If you prefer, you can also check a video version of the tutorial bellow, at my Youtube channel.

About Lambdas

In order to test lambdas, we are going to do a simple example where we will both use a lambda and a named function to achieve the same result of applying an operation over two variables.

So, we will first define a function that receives as input two values and sums them. We will use the def keyword, so it will be a named function, that we can later use in our code.

Next we will define a lambda function. To do so, we need to use the lambda keyword and the corresponding syntax, which is shown bellow.

1  lambda args: expression

So, after the lambda keyword we specify the arguments and then after “:” we put the expression for our function.

In case we have multiple arguments, we need to separate them using commas. Nevertheless, we should not enclose them in parenthesis, which will throw an invalid syntax error in MicroPython.

One important thing to note is that in the expression part of the lambda we don’t use a return statement as we would in a regular function.

This is because lambdas can only contain expressions and return is a statement [1]. You can read more about the difference of an expression and a statement in Python here.

The code

As said in the previous section, in our simple example we will create a regular function and a lambda to sum two numbers. We can define the function with the code shown bellow. It will simply receive two input parameters and return their sum.

After the definition of the function, we can later call it in our code, as we are also doing bellow.

1  def sum(x,y):
2  return (x+y)

4  result = sum (4,5)
5  print(result)


You can check bellow at figure 1 the expected result of defining and executing our sum function.

Figure 1 – Defining and executing a named function.

Now we are going to define a lambda function, accordingly to the syntax we introduced in the previous section. As mentioned before, the multiple parameters of the function (x and y) should be separated by commas but not be enclosed in parenthesis.

Then, after the “:”, we write the expression that sums both of the arguments. Nonetheless, no return statement is used, which respects the lambdas requirements.

1 sumLambda = lambda x,y: x+y
2  
3 result = sumLambda(4,5)
4 print(result)

Note that we assigned the lambda to a variable so we could later apply it to two values. Nonetheless this is different from the previous case because if we loose that lambda variable (for example, we assign it other value), our lambda will be lost, since it is not a named function. Besides that, in many practical use cases, we don’t even assign it to any variable since we simply use it as input of functions, such as the previously mentioned map, reduce and filter.

You can check the expected result bellow at figure 2, which gives the same result of executing the named function.

Figure 2 – Defining and executing a lambda function.

Final notes

In this tutorial, we saw how to create and execute a lambda function. Note that lambdas are a more advanced feature of Python / MicroPython, so it is normal that they don’t appear so often in introductory tutorials.

Although we can easily program without using lambdas, they offer a new level of flexibility and it is a concept worth learning about. Although this introductory tutorial shows a simple example that doesn’t expose how useful they are, they become a very practical tool if you spend much time writing map / reduce functions, for example.

Personally, I’ve started using lambdas while working with C# and its awesome LINQ syntax. At first, using lambdas felt complex and unnecessary, but with time it makes it easier to write code more compact and elegant. But be also careful since it will make your code harder to read for people who don’t know the lambdas syntax.


NOTE: This article is written by Nuno Santos who is an kindly Electronics and Computers Engineer. live in Lisbon, Portugal. you could check the original article here.
He had written many useful tutorials and projects about ESP32, ESP8266, If you are interested, you could check his blog to know more.

DFRobot supply lots of esp32 arduino tutorials  and esp32 projects for makers to learn.

REVIEW