«i'm going to hell for this»
– Ben Porter

seen from United States
seen from China

seen from Malaysia
seen from United States
seen from Finland

seen from United Kingdom

seen from France
seen from Norway

seen from United States
seen from Italy
seen from China
seen from China

seen from United States
seen from Germany
seen from Romania

seen from United States
seen from Oman
seen from Japan

seen from France
seen from United Kingdom
«i'm going to hell for this»
– Ben Porter
JavaScript's Lambda and Arrow Functions
New Post has been published on https://www.codesolutionstuff.com/javascripts-lambda-and-arrow-functions/
JavaScript's Lambda and Arrow Functions
The majority of contemporary programming languages support lambda expressions (Python, Ruby, Java…). Simply said, they are expressions that generate functions. First-class functions, which essentially mean sending functions as arguments to other functions or assigning them to variables, are e
Lambda functions in Python
A quick introduction to lambda functions in Python.
Below you find some examples of lambda functions in Python.
Documentation is added to the code directly – in the form of comments.
Lambda fucntions in Python are stated in accordance with the following syntax: lamda arguments : expression
# defining functions "cubing" with lambda operator
cubing = lambda x: x**3
#…
View On WordPress
ESP32 / ESP8266 MicroPython: Applying filter function to lists
ESP32 / ESP8266 MicroPython: Applying filter function to lists
The objective of this post is to explain how to use the filter function with lists in MicroPython. 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.
(more…)
View On WordPress
ESP32 / ESP8266 MicroPython: Lambda functions
ESP32 / ESP8266 MicroPython: Lambda functions
The objective of this post 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.
(more…)
View On WordPress
Python lambdas and variable scope
The issue
Recently, a colleague posted the following code snippet and asked for an explanation of why it didn't work as they expected:
x = [] for i in range(1, 10): x.append(lambda z: "%d" % i + z) print(list(x[i]("banzaii") for i in range(0, 9)))
which results in:
['9banzaii', '9banzaii', '9banzaii', '9banzaii', '9banzaii', '9banzaii', '9banzaii', '9banzaii', '9banzaii']
instead of the expected:
['1banzaii', '2banzaii', '3banzaii', '4banzaii', '5banzaii', '6banzaii', '7banzaii', '8banzaii', '9banzaii']
The explanation
The key is that the variable i is outside of the scope of the created lambda. Only the variable z is in the lambda scope. Essentially it would be like writing:
x=[] def fn(z): return "%d" % i + z for i in range(1, 10): x.append(fn)
which makes it a lot more obvious what is going on. The global variable i is used each time the function is actually called--which is after the for loop is executed and i is now 9.
The fix
The fix is simple: make sure that i is in the scope of the function that is appended to array x.
Solution 1: using a closure
def makefunc(i): def inner(z): return "%d" % i + z return inner x=[] for i in range(1, 10): x.append(makefunc(i))
In this example, a closure around the function inner passes i into the returned function's scope.
Solution 2: using lambda to create an anonymous closure
x=[] for i in range(1, 10): x.append((lambda n: lambda z: "%d" % n + z)(i))
Here, we are appending the result of calling a lambda function with the argument i which returns a lambda that uses that value. This is exactly equivalent to the previous example, but for people who like one-liners. :)
Solution 3: using a lambda with a default argument
x=[] for i in range(1, 10): x.append(lambda z, n=i: "%d" % n + z)
This solution ensures that we are using a variable n that is in the lambda's scope and is initialized by default to i. A possible issue with this solution is if the caller of the function in x[i] passes an additional argument which overrides the default value of n. With the previous solutions, this bug would raise a TypeError exception instead of running successfully with (most likely) unintended results.