In this post I will present a quick example on how ES6 can improve readability and help build code that is easier to maintain.
Also, I will be exploring partial application, a nice technique to keep in your developer toolbox.
In functional programming, applying partial application to a function means reducing the amount of arguments a function receives by fixing some of them upfront.
Let’s use a simple example to illustrate this:
I have a Logger function that receives 2 parameters:
const log = (logName, message) => {
console.log(`${logName}: ${message}`);
}
So, I can call my logger every time I want to log a message.
log('test','checking my generic log.'); // outputs: test: checking my generic log.
Most of my times, I would use the same Log Name, and here I can leverage partial application to define that log Name once:
const testLog = partial(log, 'test');
testLog('checking my test log'); //outputs: test: checking my test log
This can have multiple uses and it is a nice technique to reach when building solutions.
Let’s build a simple function that will let us use partial application in any function:
const partial = (fn, ...partialArguments) => {
return (...newArguments) => {
return fn.apply(fn, [...partialArguments, ...newArguments]);
}
}
As the title insinuates with the new ES6 features this is easier than ever. As you can see in our partial implementation we are leveraging Rest and spread operators to build the list of arguments.
Without these features, the implementation of partial would be harder to read and understand:
function partial(fn) {
var partialArguments = Array.prototype.slice.call(arguments, 1);
return function() {
var newArguments = Array.prototype.slice.call(arguments, 0);
return fn.apply(fn, partialArguments.concat(newArguments));
};
}
Not only is more imperative and harder to read, but also, leverages some tricks to access the function arguments and convert that to an array
Array.prototype.slice.call(arguments, 1);
Catch up with me on X (twitter):@juan_allo
---
@2024 Juan Manuel Allo Ron. All Rights reserved