UI Development

Currying Functions

Currying is a concept of functional programming in various programming languages. It was named after the logician ‘Haskell Brooks Curry’.


Currying is the process of chaining functions by returning a series of nested functions. Multiple arguments can be passed into a single function call by chaining the function calls one after another and passing those multiple arguments in different calls. The number of arguments passed into a function call is known as ‘arity’.

(x, y) → x * y
can be curried into
x → (y → x * y)

In other words, currying is when a function called with one argument and returns another function which is called with the next argument and that function returns another function which is called with the next argument and so on instead of passing all the arguments in a single function call.


The process of currying can easily be explained with a few examples. Let’s take a simple function and convert it into a curried function:

let volume = (length,breadth,height) =>length*breadth*height;

Here the function gets all three arguments at the same time and hence does not follow the concept of currying. Now let’s convert it into a curried function.

let volume = (length) => (breadth) => (height) => length*breadth*height;
let VolumeA = volume(10);
let VolumeB = VolumeA(15);
console.log(VolumeB(4)); //600

The above function takes one argument in every function call and hence is a perfect example of a curried function.

Let’s take another example of curried function:

function average(...args) {
    function curriedAvg(...args2) {
        return average(...args, ...args2);
    curriedAvg.value = (args.reduce((sum, currentVal) => sum + currentVal))/args.length;
    return curriedAvg;
console.log(average(5)(10)(15)(5).value); //8.75

The above example shows a curried function where we are calculating the average of any numbers and passing each number in separate function calls. The curriedAvg function takes list of arguments and saves the average of those numbers in property value. If more arguments are passed to the function, it will re-calculate the average every time and give the desired output.

Example of passing one function inside another function using currying:

const curryFn = (fn) => {
	if(fn.length === 0) {
		return fn;
	const _curriedFn  = (i, val) => (newVal) => {
		if(i - 1 === 0) {
			return fn(...val,newVal);
		return curriedFn(i - 1, [...val,newVal]);
	return _curriedFn(fn.length,[]);

const mult = (x,y) => x*y;

const curriedMult = curryFn(mult);
const multTen = curriedMult(10);
const resultMult = [10,15,18,25,50].map(multTen);
// [100,150,180,250,500]

const SI = (p,r,t) => p*r*t*0.01;
const curriedSI = curryFn(SI);
const SITimeTen = curriedSI(10);
const SIRateSix = SITimeTen(6);
const resultSI = [1000,1500,1825,2525,4250].map(SIRateSix);
// [600, 900, 1095, 1515, 2550]

The above ‘curryFn’ function is a reusable function in which we can pass any function with multiple arguments. Inside the curryFn, we have a _curriedFn which takes the depth of the function and values passed along for computation one after the other until all values are passed. In curriedMult we are passing mult function inside curryFn and then defining in a new function multTen which passes a single argument 10 to the curriedMult function and now when we pass the second argument to multTen function call it gives us the computed result. The same process is followed in curriedSI function.

Example of currying functions using bind():

function multiply(a,b,c) {
	if(b === undefined) {
		return multiply.bind(null,a)
	if(c === undefined) {
		return multiply.bind(null,a,b);
	return a*b*c;
console.log(multiply(2)(3,4)); //24
console.log(multiply(2)(5)(15)); //150
console.log(multiply(5,3,8)); //120

In the above example, we can pass all three arguments at a single time as well as we can curry above function with one argument at a time. If we pass a single argument in the first function call, the bind will help in calling the other function later when the second and third arguments are passed.

Example of currying functions using apply():

let percentage = function(...marks){
    let sum = 0;
    for(let i=0 ; i < marks.length ; i+=1 ){
        sum += marks[i];
    return ((sum/(marks.length * 100)) * 100);
let moreSubjects = function(fn, ...n){
	return function(...m){
  	return fn.apply(this, n.concat(m));

let doPercentage = moreSubjects(percentage, 95,97,92);
let againPercentage = moreSubjects(doPercentage, 85,90);
console.log(againPercentage(90, 98));

In the above example, we are calculating the percentage of a student by passing marks for different subjects in the function call. We can pass marks for any number of subjects in one function call and repeat the calling process with the percentage of other subjects. Ideally, for currying we should pass the marks for one subject in one function call, but in the above example, we are passing an array of marks in one function call and then another array of marks in next function call. Then we are concatenating both the arrays and passing the concatenated array to the main function using the apply method.


Currying doesn’t call a function, it just transforms it by returning other functions. The concept of currying is a function that returns another function until it receives all the arguments it needs and every function takes a single argument. Currying helps in the reusability of functions and helps in better code readability. Currying also makes the functions more manageable.





About The Author