Intro
In this article, we will discuss the basics of functional programming. We'll learn what is pure function, composition, currying and the closure.
Prelude
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.
Loading
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.
Loading
3. Function composition
It's just passing one function result to other functions. Great composition is always implemented with pure functions.
Loading
4. Closure
A closure occurs when a function returns another function, and this function may use the value contained in the "parent" function.
Loading
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.
Loading
Full example
Summary
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!
Comments
Add your honest opinion about this article and help us improve the content.