Blog
>
Technology

Lazy Evaluation using Function Currying and Partial Application in JavaScript

Umar Thariq
I
June 14, 2021
Indian curry dish
Photo by iMattSmart on Unsplash

Function Currying vs Partial Application in JavaScript

As JavaScript supports the nature of functional programming, it’s obvious to understand the effective usage of the functions in our code.

Currying and Partial Applications are always confusing topics and originated from mathematics and computer science.

So it’s our responsibility to understand these concepts with the clear differences and use it effectively in our projects to achieve the benefit of lazy evaluation.

Currying

Currying is the process of taking a function with multiple arguments, and turning into the sequence of functions each with a single argument.

Let’s take the below example:


import moment from "moment"
function formatDate(format, timeStamp) {
  return moment(timeStamp).format(format)
}

const DATE_TIME_FORMAT = "DD MM YYYY HH:MM"
var timeStamp1 = "2021–06–04T15:25:15.848+0000"
var timeStamp2 = "2021–06–05T07:31:38.777+0000"
var timeStamp2 = "2021–06–05T14:21:54.216+0000"

formatDate(DATE_TIME_FORMAT, timeStamp1) // "04–06–2021 20:06"
formatDate(DATE_TIME_FORMAT, timeStamp2) // "05–06–2021 13:06"
formatDate(DATE_TIME_FORMAT, timeStamp3) // "05–06–2021 19:06"

We have a generic function called formatDate which takes date_format and timestamp both as arguments and returns the formatted date.

Like the above example, we have multiple timestamps to be formatted using the same date_format. so every time while calling the formatDate function we need to pass the fresh timestamp but the same date_format again and again as it’s not changing frequently throughout our code.

Let’s use the function currying technique and change the above way of writing the formatDate function,

Function Currying example:


import moment from "moment"
const DATE_TIME_FORMAT = "DD MM YYYY HH:MM"
var timeStamp1 = "2021–06–04T15:25:15.848+0000"
var timeStamp2 = "2021–06–05T07:31:38.777+0000"
var timeStamp3 = "2021–06–05T14:21:54.216+0000"

function formatDate(format) {
  return function(timeStamp) {
    return moment(timeStamp).format(format)
  }
}

const getFormattedDate = formatDate(DATE_TIME_FORMAT)
getFormattedDate(timeStamp1); // "04–06–2021 20:06"
getFormattedDate(timeStamp2); // "05–06–2021 13:06"
getFormattedDate(timeStamp3); // "05–06–2021 19:06"

In this example, using the function currying process we curried our formatDate function. So initially it will take timeStamp as an argument and return another function. This returned function can be used to format multiple timestamps in our code until there is change of date_format.

So now every time when we need to format our timestamps, we no need to pass the date_format again and again because it’s already pre-defined in our curried function.

Partial Application

Partial application is the process of fixing the number of arguments to a function, producing another function of smaller arity (i.e no of arguments or operands taken by a function).

It is the same as Currying, but the returned functions may take one or more arguments.

In Currying, the no of functions returned is equal to no of arguments to complete the entire function evaluation, but that is not in the case of partial application function.

Let’s take the same above example, and consider now it’s taking one more extra argument called timeZone that used to append in our formatted date.


const timeZone = "IST"
const DATE_TIME_FORMAT = "DD MM YYYY HH:MM"
var timeStamp1 = "2021–06–04T15:25:15.848+0000"
var timeStamp2 = "2021–06–05T07:31:38.777+0000"
var timeStamp3 = "2021–06–05T14:21:54.216+0000"

function formatDate(format, timeZone) {
  return function(timeStamp) {
    return `${moment(timeStamp).format(format)} ${timeZone}`
  }
}

const getFormattedDate = formatDate(DATE_TIME_FORMAT, timeZone)
getFormattedDate(timeStamp1); // "04–06–2021 20:06 IST"
getFormattedDate(timeStamp2); // "05–06–2021 13:06 IST"
getFormattedDate(timeStamp3); // "05–06–2021 19:06 IST"

In this example, the formatDate function takes 2 pre-defined arguments and returns the function to get the timeStamp. Hence no of function calls is not equal to the no arguments needed to complete this evaluation.

Conclusion

Both of these techniques help to do the lazy evaluations in our code. Utilising these concepts in our day to day code, makes our code cleaner and simpler. It also avoids the repetition of passing the same arguments again and again.

References

Javascript
Functional Programming
Software Development
Front End Development

About Quinbay

Quinbay is a dynamic one stop technology company driven by the passion to disrupt technology today and define the future.
We private label and create digital future tech platforms for you.

Digitized . Automated . Intelligent