example-code-2e/17-it-generator/aritprog_v3.py

12 lines
296 B
Python
Raw Normal View History

# tag::ARITPROG_ITERTOOLS[]
import itertools
def aritprog_gen(begin, step, end=None):
first = type(begin + step)(begin)
ap_gen = itertools.count(first, step)
2021-09-16 03:48:08 +02:00
if end is None:
return ap_gen
return itertools.takewhile(lambda n: n < end, ap_gen)
# end::ARITPROG_ITERTOOLS[]