- Hands-On Reactive Programming with Python
- Romain Picard
- 452字
- 2021-06-24 18:25:19
Lambdas
Lambdas were introduced in the code sample of Chapter 1, An Introduction to Reactive Programming. They are heavily used with ReactiveX. More than that, the availability of lambdas is one of the key features that makes ReactiveX code easy to write and read. A lambda is a single-expression anonymous function. It behaves just like a function and must contain only one expression. A lambda is declared with the keyword lambda. Lambdas are very useful when a short function has to be provided to another function or a variable. In such cases, when the function to be provided is a one-liner, using lambda is easier because it avoids declaring many functions.
Here is a simple example of the usage of lambda:
def exec(f, value):
f(value)
def title(value):
print(value.title())
exec(title, "Hello world")
exec(lambda i: print(i.upper()), "Hello world")
exec(lambda i: print(i.lower()), "Hello world")
The exec function takes a function (f) and value as input parameters, and then executes the f function with value as a unique argument. In the example, the exec function is first called with a named function as an argument, the title function. The title function takes a string as a parameter and prints it in title case. This works perfectly, but it requires some boilerplate code just to print a statement.
The last two calls of the exec function use lambda instead of a function. The first call prints the string in upper case and the latter prints the string in lower case. In these two examples, there is no need to declare two other functions. The provided function object is declared inline as an input argument. There is less code to write, and reading the code is easier because the text follows the code logic. Run this example to get the following output:
With ReactiveX code, lambdas are heavily used because many small operations are chained together. Consider this other example:
from rx import Observable
a = Observable.from_(["Hello", "Goodbye"]) \
.map(lambda i: i + " the") \
.map(lambda i: i + " world") \
.map(lambda i: i.upper())
a.subscribe(lambda i: print(i))
Here, three actions are done on each item of Observable:
- Appending the the word
- Appending the world word
- Converting the whole as upper case
These three actions are trivial, so writing a dedicated function for each of them is a waste of time, space, and readability. Also, since the subscription action is also trivial, lambda can also be used here. The result of this code sample is the following one:
Each item of Observable went through the three operations before being printed, and using lambda made the code smaller and easier to read.
- Implementing Cisco UCS Solutions
- PLC控制程序精編108例
- Linux實戰(zhàn)
- FreeRTOS實時內(nèi)核應用指南
- Ganglia系統(tǒng)監(jiān)控
- Java EE 8 Design Patterns and Best Practices
- 新手學電腦從入門到精通(Windows 10+Office 2016版)
- Instant Optimizing Embedded Systems using Busybox
- STM32庫開發(fā)實戰(zhàn)指南:基于STM32F4
- Application Development in iOS 7
- 從實踐中學習Windows滲透測試
- bash shell腳本編程經(jīng)典實例(第2版)
- Linux內(nèi)核分析及應用
- Implementing Cloud Design Patterns for AWS(Second Edition)
- Mastering AWS CloudFormation