Skip to content

Instantly share code, notes, and snippets.

@etheriqa
Created March 13, 2015 15:57
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 etheriqa/5723fcab1584412b2ca8 to your computer and use it in GitHub Desktop.
Save etheriqa/5723fcab1584412b2ca8 to your computer and use it in GitHub Desktop.
mapM and mapM_ in Python
# open 0.txt, 2.txt, ..., 9.txt
# list comprehension
fs = [open('{0}.txt'.format(i)) for i in range(10)]
[f.close() for f in fs]
# map
fs = map(lambda i: open('{0}.txt'.format(i)), range(10))
map(lambda f: f.close(), fs)
# for loop
fs = []
for i in range(10):
fs.append(open('{0}.txt'.format(i)))
for f in fs:
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment