A Quick Comparision Between Imperative and Functional Programming (Part 2)

Vy Le
2 min readMar 7, 2022

--

Continue part 1. We continue with the technique pipelining of functional programming:

Photo by Ehimetalor Akhere Unuabona on Unsplash

Imagine we are operating an association of Vietnamese volunteers in Ukrainee. We have a list of cities need support. With each city, we’ll need to manage the supprotive fund as well as the list of volunteers. Here is the list:

cities = [
{
'name': 'kyiv',
'population': 2962180,
'fund': 200,
'volunteers': ['Nguyen Van A', 'Nguyen Van B', 'Nguyen Van C']
},
{
'name': 'kharkov',
'population': 2000000,
'fund': 300,
'volunteers': ['Nguyen Van D', 'Nguyen Van E', 'Nguyen Van F']
},
{
'name': 'lyiv',
'population': 2000000,
'fund': 300,
'volunteers': ['Nguyen Van G', 'Nguyen Van H', 'Nguyen Van I']
}
]

Now we have just received an amount of money from the Vietnamese all over the world and would like to distribute them evenly for each city. We also need to capitalize the name of each city. Here’s how to do it imperatively:

cities = [
{
'name': 'kyiv',
'population': 2962180,
'fund': 200,
'volunteers': ['Nguyen Van A', 'Nguyen Van B', 'Nguyen Van C']
},
{
'name': 'kharkov',
'population': 2000000,
'fund': 300,
'volunteers': ['Nguyen Van D', 'Nguyen Van E', 'Nguyen Van F']
},
{
'name': 'lyiv',
'population': 2000000,
'fund': 300,
'volunteers': ['Nguyen Van G', 'Nguyen Van H', 'Nguyen Van I']
}
]


def format_cities():
for city in cities:
city['name'] = city['name'].title()
city['fund'] = city['fund'] + 10000


format_cities(cities)
print(cities)

The function name format_cities() doesn’t explain clearly what we’re doing here. We have to read each lines of code inside format_cities() to understand what it’s doing. Even so, we have to think for a while to get their ideas exactly. This code is also hard to reuse, hard to test and hard to parallize. Compare them with this:

By reading the main function call at line 46, we understand immediately what we’re doing with the city list. In detail, we will capitalize the name of city and add fund to them. The process will happen in a sequence, cities will be handled by capitalize() and the the result is passed to add_fund(). This is called pipelining technique. We also use a higher order function called call() to generate capitalize() and add_func(). This is to combine the logic of capitalize() and add_func() into one place (they both return a new city object with an edited field).

One more thing to add, we will add one more function to the pipeline with the aim to extract neccessary fields from the city list:

def get_fields(fields):
def apply_fn(city):
return functools.reduce(lambda new_city, field: assoc(new_city, field, city[field]), fields, {})
return apply_fn


result = pipeline_each(cities, [capitalize, add_fund, get_fields(['name', 'fund', 'volunteers'])])

Thanks to higher-order function get_fields(), we can extract any fields from each city object that we want.

That’s all of the series. For a more detail explanation, pls read: https://codewords.recurse.com/issues/one/an-introduction-to-functional-programming.

--

--