Functional Programming

suggest change

Functional programming decomposes a problem into a set of functions. Ideally, functions only take inputs and produce outputs, and don’t have any internal state that affects the output produced for a given input. Below are functional techniques common to many languages, such as lambda, map, and reduce.

Lambda Function

An anonymous, inlined function defined with lambda. The parameters of the lambda are defined to the left of the colon. The function body is defined to the right of the colon. The result of running the function body is (implicitly) returned.

s=lambda x:x*x
s(2)    =>4

s(2) is 4. The => line is not Python syntax.

Map Function

Map takes a function and a collection of items. It makes a new, empty collection, runs the function on each item in the original collection and inserts each return value into the new collection. In Python 3, map returns an iterator, not a list.

This is a simple map that takes a list of names and returns a list of the lengths of those names:

name_lengths = map(len, ["Mary", "Isla", "Sam"])
print(name_lengths)    =>[4, 4, 3]

print shows a map object, such as <map object at 0x...>. list(name_lengths) is [4, 4, 3]. The => line is not Python syntax.

Reduce Function

Reduce takes a function and a collection of items. It returns a value that is created by combining the items.

This is a simple reduce. It returns the sum of all the items in the collection.

total = reduce(lambda a, x: a + x, [0, 1, 2, 3, 4])
print(total)    =>10

In Python 3, reduce is functools.reduce. Without that import this raises NameError. With the import, the total is 10. The => line is not Python syntax.

Filter Function

Filter takes a function and a collection. In Python 3 it returns an iterator of every item for which the function returned True.

arr=[1,2,3,4,5,6]
[i for i in filter(lambda x:x>4,arr)]    # outputs [5, 6]

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents