官术网_书友最值得收藏!

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 bound variable doesn't exist outside the generator expression. The variable  n isn't visible elsewhere in the program.

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.

主站蜘蛛池模板: 德令哈市| 遂溪县| 渝中区| 贡山| 青龙| 天柱县| 富宁县| 慈利县| 青田县| 五大连池市| 高密市| 阿坝县| 商水县| 屏东县| 大悟县| 拉孜县| 调兵山市| 山丹县| 曲阜市| 元江| 广汉市| 两当县| 乌鲁木齐市| 余干县| 湖南省| 昌黎县| 高碑店市| 满城县| 长兴县| 佛山市| 镇巴县| 萨嘎县| 台山市| 新建县| 宁安市| 宁武县| 海淀区| 榕江县| 古丈县| 潢川县| 如东县|