9. Functional Programming
Functional programming is just a style of programming using functions to transform data. Theoretically, functional programming has deep roots in math and is very technical. However, the easiest way to understand functional programming is to understand three main functions.
map()
transforms an item to a new onefilter()
removes an itemreduce()
collapses a collection of values into a single one
Note
Functional programming is said to be describing what
we are doing. The opposite, imperative programming
, is said to be describing how
we are doing something. Functional programming may be viewed as declarative
programming, and imperative programming may be viewed as procedural
programming.
9.1. map
Here’s an example of using map()
to transform data. Note that map()
returns an iterator and so we have to convert it to a list to observe the outputs.
1numbers = [1, 2, 3, 4, 5]
2
3output = map(lambda x: x + 1, numbers)
4output = list(output)
5
6print(output)
9.1.1. Exercise
Map the following list of integers into a new one where each element is a boolean indicating if the corresponding original element was an even number.
1numbers = [1, 2, 3, 4, 5]
Solution.
1numbers = [1, 2, 3, 4, 5]
2
3output = map(lambda x: x % 2 == 0, numbers)
4output = list(output)
5
6print(output)
9.2. filter
Here’s an example of using filter()
to remove items from a collection.
1numbers = [1, 2, 3, 4, 5]
2
3output = filter(lambda x: x % 2 == 0, numbers)
4output = list(output)
5
6print(output)
9.2.1. Exercise
Create a new list of names from the one below with names starting with ‘J’.
1names = ['Jack', 'John', 'Mary', 'Jane', 'Nancy']
Solution.
1names = ['Jack', 'John', 'Mary', 'Jane', 'Nancy']
2
3j_names = filter(lambda name: name.lower().startswith('j'), names)
4j_names = list(j_names)
5
6print(j_names)
9.3. reduce
The reduce()
function is available through the functools
module. Reduce takes the elements in a collection and collapses them into one final value. Below, we use reduce()
to sum up all the integers in a list.
1from functools import reduce
2
3numbers = [1, 2, 3, 4, 5]
4
5output = reduce(lambda x, y: x + y, numbers)
6
7print(output)
9.3.1. Exercise
Reduce the following list of numbers to the product of all the numbers.
1numbers = [1, 2, 3, 4, 5]
Solution.
1from functools import reduce
2
3numbers = [1, 2, 3, 4, 5]
4
5output = reduce(lambda x, y: x * y, numbers)
6
7print(output)
9.3.2. Exercise
Get the sum of the ages for the males and females in the data below.
1data = [
2 ('John', 32, 'Male'), ('Jack', 28, 'Male'),
3 ('Jeremy', 33, 'Male'), ('Mary', 28, 'Female'),
4 ('Nancy', 27, 'Female'), ('Katherine', 33, 'Female')
5]
Solution.
1from functools import reduce
2
3data = [
4 ('John', 32, 'Male'), ('Jack', 28, 'Male'),
5 ('Jeremy', 33, 'Male'), ('Mary', 28, 'Female'),
6 ('Nancy', 27, 'Female'), ('Katherine', 33, 'Female')
7]
8
9items = filter(lambda tup: tup[2] == 'Male', data)
10items = map(lambda: tup[1], items)
11male_total_age = reduce(lambda x, y: x + y, items)
12
13items = filter(lambda tup: tup[2] == 'Female', data)
14items = map(lambda: tup[1], items)
15female_total_age = reduce(lambda x, y: x + y, items)
16
17print(male_total_age)
18print(female_total_age)