Skip to content

Instantly share code, notes, and snippets.

@kimmobrunfeldt
Created May 20, 2013 08:48
Show Gist options
  • Select an option

  • Save kimmobrunfeldt/5611113 to your computer and use it in GitHub Desktop.

Select an option

Save kimmobrunfeldt/5611113 to your computer and use it in GitHub Desktop.
Haskell-style infinite "list" testing with Python.
"""
Haskell-style infinite "list" testing.
"""
def infinite_range(step):
i = 0
while True:
yield i
i += step
def take(count, iterator):
a = []
for x in xrange(count):
try:
a.append(iterator.next())
except StopIteration:
pass
return a
def main():
print take(5, infinite_range(1))
print take(10, infinite_range(5))
million_items = take(10**6, infinite_range(1))
print len(million_items)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment