Closures, currying, function composition as your new friends

created: 5 months ago
updated: 3 months ago
new
  • functional programming

Intro

In this article, we will discuss the basics of functional programming. You will learn what is pure function, composition, the currying technique and the closure mechanism.

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.
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!