Functional Programming & Higher-Order function

Ridwan Yusuf
4 min readJan 30, 2023

--

Functional programming paradigm favors the use of functions and immutable data structures(i.e.the idea that data cannot be changed once it’s defined) to build applications. It also puts emphasis on modularity, the ability to easily build larger systems composing well-defined functions.

Though Python is not a strictly functional programming language, it contains features commonly used in functional programming such as higher-order functions and immutability.

Higher-order functions are functions that take another functions as arguments or return functions as their results. In functional programming, functions are first-class citizen, meaning that they can be assigned to a variables and passed as arguments to another function. Python supports these features natively, making it a suitable choice for functional programming.

Below are some built-in higher-order functions provided by Python that are commonly found in functional programming languages.

1. filter():

The filter() function allows you to filter items from an iterable(check out the guide on generator, it explains iterables) based on a test function provided to check if an item meets the pass criteria.

In essence, filter() takes a function and an iterable as arguments:
Here is the syntax: filter(function, iterable)
The function passed into filter() is expected to return True or False indicating whether the element/item should be included in the final result

Say we have a list(i.e. an iterable) of numbers and we need to filter out those that are greater than three:
[*]snip

my_list = [1,2,3,4,5,6,7,8,9, 10]

def is_greater_than_three(number):
return number > 3

filtered_result = filter(is_greater_than_three, my_list)

print(list(filtered_result))

[*]Output
[4, 5, 6, 7, 8, 9, 10]

Great! You may be wondering why using list on the filtered_list. That’s because the returned output is an iterator which means we can access the next element using built-in next() function or just looping through it.
[*]snip

iterator = filter(is_greater_than_three, my_list)
print(next(iterator))
print(next(iterator))

[*]Output
4
5

Check out comprehensions using this link:
List comprehension equivalent:
[*]snip

[item for item in my_list if item > 3]

2.map():

map() function takes a function and an iterable as arguments. It applies the function provided to each item in the iterable and returns an iterator containing the results gotten after the function is applied to each item.
Syntax: map(function, iterable)

Say we need to generate square of numbers in a list:
[*]snip

my_list = [2,3,4,5,6,7,8]

def square(number):
return number * number

results = map(square, my_list)

print(list(results))

[*]Output
[4, 9, 16, 25, 36, 49, 64]

As usual we needed to use list() to turn the results, which is an iterator, to list.
Comprehension version:

[item * item for item in my_list]

It’s worth noting that map() function can take more than one iterable. In such case, the number of iterables passed to map() must match the number of arguments the function expects:
Syntax: map(function, iterable_1, iterable_2)

[*]snip

list_1 = [1,3,5,7,9]
list_2 = [2,4,6,8,10]

def sum_numbers(number1, number2):
return number1 + number2

results = map(sum_numbers, list_1, list_2)

print(list(results))

[*]Output]
[3, 7, 11, 15, 19]

3. lambda:

lambda functions are anonymous functions that can be used to write short functions. Lambda in Python provides a convenient way to define anonymous functions without specifying a name and using the ‘def’ keyword.

[*]snip

multiply = lambda x,y : x * y 
print(multiply(2,3))

[*]Output
6

This is equivalent to writing as:

def multiply(x,y):
return x * y

It is also possible to provide the arguments needed in lambda expression straight away:
[*]snip

result = (lambda x,y : x * y)(2,3) 
print(result)

[*]Output
6

Let’s try another example. You still remember the filter() function discussed earlier which is used to filter out some items from a list.
We are going to use the filter with an anonymous function:
[*]snip

my_list = [1,2,3,4,5,6,7]

greater_than_three = filter(lambda x : x > 3, my_list)
print(list(greater_than_three))

[*]Output
[4, 5, 6, 7]

We created a lambda function instead of creating a new function outside of filter() function.

reduce():

The reduce() function accepts a function and an iterable as arguments. It allows the function passed to it to operate on each items in an iterable with the end goal of producing just a single value after all computations. reduce() reduces an iterable to a single value.
syntax: reduce(function, iterable)
[*]snip

from functools import reduce

def add(x, y):
return x + y

x = reduce(add, [1,2,3,4])
print(x)

[*]Output
10

The reduce() reduced the iterable into a single value by adding all items together

Let’s replace the add() function with a lambda function:

[*]snip

x = reduce(lambda x,y: x+y, [1,2,3,4])
print(x)

[*]Output
10

We can simply sum all the numbers using the built-in sum() function

sum([1,2,3,4])

As we can see, Python provides comprehensions that solve the same problems as some higher-order functions.

Thanks for reading and feel free to explore my video collection on YouTube for more educational content. Don’t forget to subscribe to the channel to stay updated with future releases.

--

--

Ridwan Yusuf
Ridwan Yusuf

Written by Ridwan Yusuf

RidwanRay.com -I provide engineers with actionable tips and guides for immediate use

No responses yet