Article thumbnail

Closures, currying, function composition as your new friends

functional programming


In this article, we will discuss the basics of functional programming. We'll learn what is pure function, composition, currying and the closure.


When writing functional code, you can get the impression that it is very difficult to understand at first. I agree with that.

However, such code has many interesting features that can be used in everyday work. Once you have mastered the basics, you will begin to notice how powerful this tool is.

1. Looking at not pure function

This function is not pure because it has side effects, modifies a variable outside scope, returns different values ​​each time and modifies input parameters.


2. Pure function

A pure function always returns the same values ​​for the same arguments, does not modify input parameters and has no side effects like asynchronous code or changing something outside its range.


3. Function composition

It's just passing one function result to other functions. Great composition is always implemented with pure functions.


4. Closure

A closure occurs when a function returns another function, and this function may use the value contained in the "parent" function.


5. Currying

In simple words, transforming one multi-argument function to many unary functions. We can do it ourselves or write a function to do it for us.


Full example


I hope the basics of functional programming are now easy for you 🙂.

Play with the example code. Practice is necessary to understand it completely and to notice the benefits.

Feel free to contact me if you have any questions/proposals. Have a nice day and good health!

I create content regularly!

I hope you found my post interesting. If so, maybe you'll take a peek at my LinkedIn, where I publish posts daily.


Add your honest opinion about this article and help us improve the content.

created: 12-05-2022
updated: 02-07-2022