Last active
August 29, 2015 13:56
-
-
Save adonoho/9228763 to your computer and use it in GitHub Desktop.
Counters, Closures and Generators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import collections | |
__author__ = 'Andrew W. Donoho, <github@store.ddg.com>' | |
__copyright__ = 'Public Domain' | |
def counter() -> collections.Iterator: | |
def count() -> int: | |
n = 0 | |
while n < 10: | |
n += 1 | |
yield n | |
return iter(count()) | |
def counter2(count: int) -> collections.Iterator: | |
n = 0 | |
while n < count: | |
n += 1 | |
yield n | |
def main(): | |
for i in counter(): | |
print(i) | |
for i in counter2(5): | |
print(i) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment