Providing function with fewer arguments than it expects is called Partial Application of functions.
Often I am asked: “Why in the world would you apply a function partially?”
“Because the logic I get after that is a thing of Beauty and Functional Purity.”
We call a function with fewer arguments than it expects and It returns a function that takes the remaining arguments. And this is called Partial Application of Functions.
This style of Partial Application is not functionally cool, but I still want you to know about this.
We created a function ‘add’ which accepts two arguments.
We preloaded it with a single argument and created a function ‘increment’ which takes only one argument.
We created another function by the same process of Partial Application, this time by applying a different argument.
At last, we called our preloaded functions with the final argument.
Binding a function to fewer arguments helps us in generating other functions to make our code less repetitive and more definitive. But there are some problems with that, and those are apart from not being functionally cool.
A curried function is always pure as it generates the same function for the same input.
Currying is a Batman’s utility belt for a functional programmer and we’ll see how it’s important.
Now, I want you to have a look at this code.
This construct is called Method Chaining and is known to be a good Object Oriented Design. Now, if you look closer you can see how it’s working and what it lacks.
‘Hello’, the data is being piped through the chain of compatible methods and we can chain more methods as long as the returned object is compatible with the method being chained.
So, this can go on like this.
The above construct is possible because of the ‘Hello’ object provided, all the methods that are chained are worthless without the object provided to them. That’s sad. The problem with this or I can say with every Object Oriented Design is that everything revolves around Objects, everything depends on data.
Let’s functionalise this,
Well, this looks a little less readable, don’t worry it’s functional programming, For cases like this, we have Function Composition.
Function composition is the process of combining two or more functions to produce a new function.
Okay, so we can use Function Composition to combine our replace and concat functions, for that we need a composer function.
Let’s compose our functions, but wait we have a problem, our composer works on functions with only one parameter but our functions replace and concat clearly takes more than one parameter.
Time for our favorite technique, currying
Now, I need you to carefully examine how I strategically curried my functions so that the only argument remaining to be applied is the “data” to operate upon.
And this can go on like this,
And this is cool at so many levels. For Example, since our functions no longer depend upon the data provided, we can do this.
I am so sorry, I mean
Hey, congratulations we just wrote a point-free style function. Do you see no “ . ”.
Well, point-free style functions are those that do not mention data upon which they operate. This style of writing functions is called Point-free style or Tacit Programming. As per Wikipedia
Tacit programming, also called point-free style, is a programming paradigm in which function definitions do not identify the arguments (or “points”) on which they operate. Instead, the definitions merely compose other functions, among which are combinators that manipulate the arguments.
Currying and Composition play very well to create this style.
Currying prepares functions to take only data as their argument(the rest arguments being partially applied beforehand) and Composition helps in combining those partially applied functions so data can pipe through them.
And this style, this point-free style of writing functions is a litmus test of Functional Purity. As it confirms that our code constitutes of smaller Pure Functions otherwise we won’t be able to compose them.
See, I told you the results are Pure and that’s why you apply functions partially.
Written with 💖.
Thanks for reading.
Create your free account to unlock your custom reading experience.