Skip to content

Instantly share code, notes, and snippets.

@podlech
Created June 12, 2018 03:56
Show Gist options
  • Save podlech/d73629265d8af36ed6f36fe7b017dcb3 to your computer and use it in GitHub Desktop.
Save podlech/d73629265d8af36ed6f36fe7b017dcb3 to your computer and use it in GitHub Desktop.
iterators.py
from functools import reduce
arr = [1, 2, 3]
map1 = map((lambda x: (print("inside map1: %s" % (x + 1)) or x + 1)), arr)
map2 = map((lambda x: (print("inside map2: %s" % (x + 2)) or x + 2)), map1)
print("sum: " + str(reduce((lambda x, y: x + y), map2, 0)))
print(sorted(map2)) # gives [] since iterator has been consumed
"""
inside map1: 2
inside map2: 4
inside map1: 3
inside map2: 5
inside map1: 4
inside map2: 6
sum: 15
[]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment