JS interview in 2 minutes / Currying ?

Question:
What is currying in JavaScript?

Quick answer:
It is a technique used to convert a function that takes multiple arguments into a chain of functions where every only takes one argument.

Longer answer:
Currying is basically all about Higher Or…


This content originally appeared on DEV Community and was authored by Nikita Kozlov

Question:
What is currying in JavaScript?

Quick answer:
It is a technique used to convert a function that takes multiple arguments into a chain of functions where every only takes one argument.

Longer answer:
Currying is basically all about Higher Order Functions. It is an application of JavaScript's ability to return functions from other functions.

We are replacing a function that takes n arguments with a set of n functions, which applied one by one gives exactly the same answer as original functions.

We can learn it by example right away. Btw it feels like this one is the most common one.

// regular implementation
function add1(a, b) {
  return a + b;
}

console.log(add1(2,5)) // 7

// curried implementation
function add2(a) {
  return function(b) {
    return a + b
  }
}

console.log(add2(2)(5)) // 7

That is it. This is what currying is.

Real-life applications:
At first glance, it may look a bit weird ? Why do we ever need to call a function separating arguments?

You can think of it as a function preparation for execution. If you have some common operation, e.g. getting an object property, you can move it to a curried version.

function searchUser {
  // ...
  user['id']
  // ...
}

function updateUser {
  // ...
  user['id']
  // ...
}

// user['id'] can be refactored with
let getUserId = user => user['id']

// Or you can go even further and implement generic getter
let pluck = name => object => object[name]
let getUserId = pluck('id')
let getUserName = pluck('name')

let name = getUserName(user)

So functions like this can be joined to some helpers library. And here is RxJS.pluck and here is Ramda.pluck.

Have a good curry ?

Resources:
wiki/Currying

Other posts:

Btw, I will post more fun stuff here and on Twitter. Let's be friends ?


This content originally appeared on DEV Community and was authored by Nikita Kozlov


Print Share Comment Cite Upload Translate Updates
APA

Nikita Kozlov | Sciencx (2021-05-14T09:21:47+00:00) JS interview in 2 minutes / Currying ?. Retrieved from https://www.scien.cx/2021/05/14/js-interview-in-2-minutes-currying-%f0%9f%a5%98/

MLA
" » JS interview in 2 minutes / Currying ?." Nikita Kozlov | Sciencx - Friday May 14, 2021, https://www.scien.cx/2021/05/14/js-interview-in-2-minutes-currying-%f0%9f%a5%98/
HARVARD
Nikita Kozlov | Sciencx Friday May 14, 2021 » JS interview in 2 minutes / Currying ?., viewed ,<https://www.scien.cx/2021/05/14/js-interview-in-2-minutes-currying-%f0%9f%a5%98/>
VANCOUVER
Nikita Kozlov | Sciencx - » JS interview in 2 minutes / Currying ?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/14/js-interview-in-2-minutes-currying-%f0%9f%a5%98/
CHICAGO
" » JS interview in 2 minutes / Currying ?." Nikita Kozlov | Sciencx - Accessed . https://www.scien.cx/2021/05/14/js-interview-in-2-minutes-currying-%f0%9f%a5%98/
IEEE
" » JS interview in 2 minutes / Currying ?." Nikita Kozlov | Sciencx [Online]. Available: https://www.scien.cx/2021/05/14/js-interview-in-2-minutes-currying-%f0%9f%a5%98/. [Accessed: ]
rf:citation
» JS interview in 2 minutes / Currying ? | Nikita Kozlov | Sciencx | https://www.scien.cx/2021/05/14/js-interview-in-2-minutes-currying-%f0%9f%a5%98/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.