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, ...

Get Functional Python Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.