Skip to content

Instantly share code, notes, and snippets.

@adonoho
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adonoho/9228763 to your computer and use it in GitHub Desktop.
Save adonoho/9228763 to your computer and use it in GitHub Desktop.
Counters, Closures and Generators
#!/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