Map, Filter, and Reduce are paradigms of functional programming. They allow us to write simpler, shorter code, without necessarily needing to bother about intricacies like loops and branching.
This post will demonstrate how to run another Python feature in Python4Delphi with RAD Studio: Map, Filter, and Reduce in Python GUI apps and gets the output. We will discuss them one by one and understand their use cases.
Prerequisites: Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in this video Getting started with Python4Delphi.
First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.
Table of Contents
1. Map
Map
applies a function to all the items in an input_list. The map()
function iterates through all items in the given iterable and executes the function
we passed as an argument on each of them.
Here is the Pseudocode:
1 |
map(function_to_apply, list_of_inputs) |
Most of the times we want to pass all the list elements to a function one-by-one and then collect the output. For instance:
1 2 3 4 5 |
items = [0, 1, 2, 3, 4, 5] squared = [] for i in items: squared.append(i**2) |
Map
allows us to implement this in a much simpler and elegant way:
1 2 |
items = [0, 1, 2, 3, 4, 5] squared = list(map(lambda x: x**2, items)) |
Most of the times we use lambdas with map
so we did the same. Instead of a list of inputs we can even have a list of functions! Here is the nice example:
1 2 3 4 5 6 7 8 9 |
def multiply(x): return (x*x) def add(x): return (x+x) funcs = [multiply, add] for i in range(6): value = list(map(lambda x: x(i), funcs)) print(value) |
Output in Python GUI:
2. Filter
As suggested by its name, the filter creates a list of elements for which a function returns true. Here is a short and concise example:
1 2 3 |
number_list = range(-5, 5) less_than_zero = list(filter(lambda x: x < 0, number_list)) print(less_than_zero) |
The filter resembles a for loop but it is a built-in function and faster. Let’s see the output in our Python GUI:
3. Reduce
Reduce
is a really useful function for performing some computation on a list and returning the result. It applies a rolling computation to sequential pairs of values in a list. For example, if you wanted to compute the product of a list of integers.
The example below is the normal way you might go when doing this task in Python (using a basic for loop):
1 2 3 4 5 |
product = 1 list = [1, 2, 3, 4] for num in list: product = product * num |
Note that in Python GUI, to get the response, we need to print the results. Let’s rewrite it with reduce:
1 2 3 4 |
from functools import reduce product = reduce((lambda x, y: x * y), [1, 2, 3, 4]) print(product) |
Let’s see the output in our Python GUI:
4. Conclusion
As mentioned previously; Map, Filter, and Reduce are convenience functions. They are there so we can avoid writing more cumbersome code, but avoid using both them and lambda expressions too much.
Don’t force these tools because “we can”, as it can often lead to illegible code that’s hard to maintain. Use them only when it’s absolutely clear what’s going on as soon as we look at the function or lambda expression.
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.