- Functional Python Programming
- Steven F. Lott
- 332字
- 2021-08-27 19:20:20
Using a functional hybrid
We'll continue this example with a mostly functional version of the previous example to compute the sum of multiples of three and five. Our hybrid functional version might look like the following:
print(sum(n for n in range(1, 10) if n%3==0 or n%5==0))
We've used nested generator expressions to iterate through a collection of values and compute the sum of these values. The range(1, 10) method is iterable and, consequently, a kind of generator expression; it generates a sequence of values . The more complex expression n for n in range(1, 10) if n%3==0 or n%5==0 is also an iterable expression. It produces a set of values,
. The variable n is bound to each value, more as a way of expressing the contents of the set than as an indicator of the state of the computation. The sum() function consumes the iterable expression, creating a final object, 23.
The if clause of the expression can be extracted into a separate function, allowing us to easily repurpose this for other rules. We could also use a higher-order function named filter() instead of the if clause of the generator expression. We'll save this for Chapter 5, Higher-Order Functions.
The variable n in this example isn't directly comparable to the variable n in the first two imperative examples. A for statement (outside a generator expression) creates a proper variable in the local namespace. The generator expression does not create a variable in the same way as a for statement does:
>>> sum(n for n in range(1, 10) if n%3==0 or n%5==0) 23 >>> n Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'n' is not defined
The variable n doesn't exist outside the binding in the generator expression. It doesn't define the state of the computation.
- UI圖標創意設計
- Facebook Application Development with Graph API Cookbook
- C語言程序設計案例教程(第2版)
- Java 9 Concurrency Cookbook(Second Edition)
- Visual Basic 6.0程序設計計算機組裝與維修
- 軟件測試項目實戰之性能測試篇
- 微信小程序全棧開發技術與實戰(微課版)
- Deep Learning with R Cookbook
- Django Design Patterns and Best Practices
- C語言程序設計實踐
- LabVIEW入門與實戰開發100例(第4版)
- Effective C++:改善程序與設計的55個具體做法(第三版)中文版(雙色)
- Flink原理深入與編程實戰:Scala+Java(微課視頻版)
- 深入淺出Go語言核心編程
- Mastering Kali Linux for Advanced Penetration Testing(Second Edition)