LogoJuan Manuel Allo Ron

Partial Application made easy with ES6

By Juan Manuel Allo Ron on Nov 12, 2018

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.

What is partial application?

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:

  • Log Name
  • Message
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.

General Partial Application function

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 you can see, our partial function receives the original fn and a set of arguments. Those will be the ones fixed.
  • Then, it returns another function that can receive more arguments.
  • Finally, merges all the arguments together when calling the original function.
  • We leverage apply to send the arguments to the original function and call it.

Why is this easier now?

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

Development
Es6
Functional programming
Javascript
Partial application

Share

X Facebook Y Combinator LinkedIn Reddit Email

---

Similar Articles

Promise States: Pending/Fulfilled/Rejected

Javascript as Promised

Nov 15, 2017
Promises have been there for a while now, officially released in ES6 but already being supported by most browsers before that and/or polyfiled by libraries
Blurred lines of code

Callbacks in Javascript

Nov 15, 2017
Quick recap on what a JS callback is and the common problems around using them
A pattern formed with the null word

Tip: Watch out for null in default params

Jun 4, 2020
Defaults in function parameters is a feature that simplified my code a lot. But there is one caveat that we need to be aware of! Let’s start with an example:
React Logo

Learning Path: Getting started with React

May 19, 2020
In this post I want to share some resources that helped me learn React and I hope they are useful for anyone starting out there.

@2024 Juan Manuel Allo Ron. All Rights reserved