Skip to content

Instantly share code, notes, and snippets.

@mjard
Forked from desg/gist:3137477
Created July 18, 2012 17:05
Show Gist options
  • Save mjard/3137490 to your computer and use it in GitHub Desktop.
Save mjard/3137490 to your computer and use it in GitHub Desktop.
# pythonic
def map(source, func):
dest = []
for x in source:
dest.append(func(x))
return dest
# list comprehension
def map(source, func):
return [func(x) for x in source]
# lambda
map = lambda s,f: [f(x) for x in s]
# generator
def map(source, func):
for x in source:
yield func(x)
# desg
def map(list,func):
x = 0
for len(list) in x:
new_list = new_list.append(func(list[x]))
x += 1
return new_list
# desg that works
def map(source, func):
dest = list() # this is why we don't name things list
for i, x in enumerate(source):
dest.append(func(source[i]))
return dest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment