In [17]:
# map, performs a func over each element in sequence literal, 
# and returns a list with the function performed on it
def show_me(x):
    print x
    return x

m = map(show_me, [1, 2, 3])

def farenheit(x):
    return (9.0/5.0) * x + 32

map(lambda x: (9.0/5) * x + 32, [30, 32])
map(farenheit, [20, 24, 27])


1
2
3
Out[17]:
[68.0, 75.2, 80.6]

In [14]:
def squared(x):
    return x**2

map(squared, [2, 4, 6])


Out[14]:
[4, 16, 36]

In [23]:
# reduce performs a function over each element, passing over the result
# from the prior execution, to the next one
def app(n, w):
    return n + w
print reduce(app, ['H','E','L','L','O'])
print reduce(app, [1, 1, 2, 3, 4, 5, 6, 8])
print sum([1, 1, 2, 3, 4, 5, 6, 8])


HELLO
30
30

In [26]:
def larger(a, b):
    return a if a > b else b
reduce(larger, [231, 142, 904, 2, 12, -12, 39])

# the lambda equiv
reduce(lambda a,b: a if a > b else b, [3, 5,10,1, 3, 4, 1, 9])


Out[26]:
10

In [28]:
# filter, like reduce and map, takes in a function and an iterable
# filter will locate all elements in the iterable that match True
def even(n):
    return n % 2 == 0

filter(even, [2, 3, 4, 5, 6, 7])

filter(lambda x: x % 2 == 0, [2, 4, 2123, 1235, 5212, 9, 8])


Out[28]:
[2, 4, 5212, 8]

In [ ]: