Functional programming

Functional Programming goes hand-in-hand with Immutable data structures

A chapter on immutable data structures!

Vishal Sharma
The Startup
Published in
4 min readJun 30, 2020

--

Photo by Jefferson Santos on Unsplash

While designing a programming language, the creators choose to emphasize one particular approach to programming. This has given birth to many programming styles — procedural, declarative, object-oriented, and many more. Functional Programming is one such approach that basically decomposes a problem into a set of functions.

When you hear Functional Programming aka FP, Haskell is the language that rings the bells. But, most of the Pythonistas use this approach while writing code because it is easy to understand and keeps the code clean.

So, why should you learn functional programming? There is no hard and fast rule that you must know functional programming if you are working with Python. But, it is just another tool in your toolbox that you can use in many scenarios. Let’s start then!

What is Functional Programming?

Well, FP or Functional Programming is a technique where your program mainly does computations mainly while evaluating a function. And, it mainly relies on the immutable data structure.

Still confused? FP is just a programming style like you might have seen Object-Oriented style in C++ or Java. And, the beautiful essence of FP is in its likelihood to minimize the number of bugs in the code. Hence, making a more maintainable piece of code.

Trying a mutable structure approach

Let’s dive into the code to know more about it.

got_char=[
{"name":"John","profession":"King's Hand","experience_in_year":8},
{"name":"Arya Stark","profession":"No One","experience_in_year":2}
]

I have taken a list of dictionaries. But, if you notice, I have taken a mutable data structure which means if I change any name from the dictionary, it will reflect that change in the original dictionary.

got_char[0]["name"]="Sansa"

And, now if I print out the got_char list of dictionaries. The output will look like:

[Output]:[
{"name":"Sansa","profession":"King's Hand","experience_in_year":8},
{"name":"Arya Stark","profession":"No One","experience_in_year":2}
]

I actually modified the name of a character and it kind of screwed our data. If you remember, in the beginning, I wrote that “Functional Programming ideally goes hand-in-hand with immutable data structures.” So, you want a data structure than can’t be modified.

Now, I will give a try to a structure that can’t be modified i.e immutable data structure. That leads to a cleaner conceptual model as I will be forced to do computations in a certain way. Also, I could have the history of all the computations that I do on the object. If I need to change the structure, I would have to create a full copy of the object and do the changes.

The gist is that immutable data structures are the core of functional programming in Python.

Also, if you look at the declaration of the list of dictionaries, I had to give “Keys” again and again in every dictionary. I have bold them in the following code.

got_char=[
{"name":"John","profession":"King's Hand","experience_in_year":8},
{"name":"Arya Stark","profession":"No One","experience_in_year":2}
]

Now, if I add more characters to it, I will have to write these keys again. Hence, it becomes a tedious task. I tried to go with namedtuple to overcome this issue.

Going with Immutable Data Structures

import collections
got_char=collections.namedtuple('got_char', ['name','profession','experience_in_year']
)

After creating a namedtuple, you can assign values to the got_char.

John=got_char("name":"John","profession":"King's Hand","experience_in_year":8)John.name
[Output]: 'John'
John.profession
[Output]: 'King's Hand'

Now, if I try to change the name, it will throw an attribute error.

John.name='Sansa'
AttributeError: can't set attribute

We have finally got a way to create a dataset in an immutable data structure. Now, let’s create more rows for our dataset.

got_chars=[
got_char("name":"John","profession":"King's Hand","experience_in_year":8),
got_char("name":"Arya Stark","profession":"No One","experience_in_year":2)
]

This time I have created a list of namedtuple objects. There still lies a problem. Yes, I can’t modify the immutable namedtuple objects. But, I can delete the rows from the list.

del got_chars[0]

This command will delete the first namedtuple object from the list. And, this also messes up the data. To avoid it, I decided to take a tuple of namedtuple objects.

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)
)

And, this time when I tried to delete the first namedtuple object, I got an error.

del got_chars[0]
TypeError: 'tuple' object doesn't support item deletion

Finally, I have created a list of GOT characters in which I can’t go in and mess up the data. But, I can still access everything.

And, we want that only for a solid functional programming approach — the core of immutable structures. This post explores the benefit of using immutable data structures if you are trying to work with a functional programming style.

Peace!

--

--