Created
July 6, 2011 14:34
-
-
Save ericmoritz/1067375 to your computer and use it in GitHub Desktop.
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
generating 0 | |
mapping 0 | |
generating 1 | |
mapping 1 | |
generating 2 | |
mapping 2 | |
generating 3 | |
mapping 3 | |
generating 4 | |
mapping 4 | |
generating 5 | |
mapping 5 | |
generating 6 | |
mapping 6 | |
generating 7 | |
mapping 7 | |
generating 8 | |
mapping 8 | |
generating 9 | |
mapping 9 | |
['mapped 0', 'mapped 1', 'mapped 2', 'mapped 3', 'mapped 4', 'mapped 5', 'mapped 6', 'mapped 7', 'mapped 8', 'mapped 9'] | |
-- | |
generating 0 | |
Traceback (most recent call last): | |
File "starmap_vs_imap.py", line 20, in <module> | |
print list(imap_gen) | |
TypeError: mapper() takes exactly 2 arguments (1 given) |
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
from itertools import imap, starmap | |
def gen(): | |
for i in range(10): | |
print "generating %s" % i | |
yield (i, i*2) | |
def mapper(item, item_squared): | |
print "mapping %s" % (item, ) | |
return "mapped %s" % (item) | |
starmap_gen = starmap(mapper, gen()) | |
imap_gen = imap(mapper, gen()) | |
print list(starmap_gen) | |
print "--" | |
print list(imap_gen) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment