nhrksa
Last Updated: February 25, 2016
·
257
· bt3gl
1396323302436

Python Functional Stuff

A crash course.

Python has First Class Functions

def create_adder(x):
    def adder(y):
        return x + y
    return adder

>>> add_10 = create_adder(10)
>>> add_10(3)  
13

Python has Anonymous Functions

(lambda x: x > 2)(3)   # => True

Python has Built-in Higher Order Functions

map(add_10, [1, 2, 3])   # => [11, 12, 13]
filter(lambda x: x > 5, [3, 4, 5, 6, 7])   # => [6, 7]
Say Thanks
Respond