- 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.
- Windows Vista基礎與應用精品教程
- Puppet實戰
- Arch Linux Environment Setup How-to
- VMware Horizon View 6 Desktop Virtualization Cookbook
- Windows 7案例教程
- Mastering Reactive JavaScript
- Linux內核設計的藝術:圖解Linux操作系統架構設計與實現原理
- Linux軟件管理平臺設計與實現
- OpenVZ Essentials
- 應急指揮信息系統設計
- VMware Horizon Mirage Essentials
- Docker容器技術與應用
- 鴻蒙入門:HarmonyOS應用開發
- Mastering Spring Cloud
- Linux應用大全 服務器架設