Different types of currying and how to use them for problem solving

I don’t need the whole world, just you

Let’s look at the example of a curry function in JavaScript:

const curry = (fn) =>     
function curried(...args) {
const haveEnoughArgs = args.length >= fn.length
const partiallyApplied = (...moreArgs) =>
curried(...args.concat(moreArgs))

if (haveEnoughArgs) return fn(...args)
return partiallyApplied
}
  • curry(someFunction(1)(2)(3))
  • curry(someFunction(1, 2, 3))
  • curry(someFunction(1, 2)(3))
  • curry(someFunction(1)(2))


In this article I’ll quickly introduce you to currying, it’s purpose, and show you how to understand it with plain and simple code.

What is currying?

Currying is the process of converting a function of multiple arguments to a chain of functions of one argument. Like so:

const createUser = (firstName, lastName) => 
`${firstName} ${lastName}`
// becomes
const createUser = (firstName) => (lastName) =>
`${firstName} ${lastName}`

Why do we need this?

Curried functions are easy to partially apply, and therefore easy to reuse in slightly different scenarios. Imagine we need to create a user like in the example above. Ok…


  • классное!
  • представляет основы любой математической системы, включая программирование!
  • помогает Вам понять функциональное программирование на более глубоком уровне


  • is awesome!
  • represents the foundations of any mathematical system, including programming!
  • helps you to understand functional programming more deeply


Why?

Why would I want to? Because code is for humans, not machines. The closer it is to plain English, the easier it will be for you and your team to maintain and improve it.

Ivan Korolenko

https://ivankorolenko.com | Senior front end engineer | Turning wishes into software

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store