Functional Programming: Reduce() Function in Python

Functional Programming: Reduce() Function in Python

Reducing sequence to a single output value! Functional Programming: Reduce() Function in Python. In the previous string of articles, I have talked about the importance of immutable data structures in functional programming. I have also covered the filter() function in the last article. Let’s start from where we left. In this article, I will be talking about the reduce() function.

In the previous string of articles, I have talked about the importance of immutable data structures in functional programming. I have also covered the filter() function in the last article.

Let’s start from where we left. In this article, I will be talking about the reduce() function.

Working with reduce() function

Just like the filter() function, reduce is another FP aka functional programming primitive that you can use in your Python code. The documentation defined reduce() function as:

“Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value.”

reduce(functioniterable[, _initializer_]): Looks like reduce() takes a function and an iterable (sequence of stuff). Although an initializer can also be given as a parameter and is placed before the items of the sequence in the calculation.

For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence.

It might seem a lot to grab. For a beginner, reduce() function gives us a descriptive value (eg. sum). It reduces a sequence to a single output value.

reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
[Output]: 15

The above line of code took a lambda function and calculated the sum of the sequence given as a parameter. “x” is the accumulated value from each iteration and “y” is the new value. In the end, it gave us the sum of the sequence.

Now that you have got the gist of the reduce() function. It’s time to bring our last problem in the picture and work with it only. Our dataset consists of GOT characters — name, profession, and experience.

got_chars=(
got_char("name":"John","profession":"King's Hand","experience_in_year":3),
got_char("name":"Arya Stark","profession":"No One","experience_in_year":4),
got_char("name":"Sansa Stark","profession":"Queen Winterfell","experience_in_year":1),
got_char("name":"Danaerys","profession":"Queen","experience_in_year":3)
)

technology python coding programming functional-programming reduce

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Guide to Python Programming Language

Guide to Python Programming Language

Generate QR Code using Python — Python Programming — PyShark

This article will explore how to generate QR code in Python and some useful creation features from pyqrcode library. QR codes recently became more popular than ever before, yet few people know that the first iterations of QR codes were created back in 1990s in Japan for the automotive industry.

Are functions from programming really functions?

Are functions from programming really functions? If you are reading this, then most probably you already know quite well what functions are in programming.

Python Programming: A Beginner’s Guide

Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?

Python's reduce(): From Functional to Pythonic Style – Real Python

Python's reduce(): From Functional to Pythonic Style – Real Python