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

In this article I will tell you about different types of currying in JavaScript. To understand them, you will need to know about classical currying. You can read my article on that topic and then proceed with this one.

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
}

It returns a function that can handle cases like this:

  • curry(someFunction(1)(2)(3))
  • curry(someFunction(1, 2, 3))
  • curry(someFunction(1, 2)(3))
  • curry(someFunction(1)(2))

What we would like to add…


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}`

But it’s not comfortable to do those manipulations manually every time we need to break down our functions througn currying. So we make curry function that can do it for us.

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…


Эта статья призвана описать лямбда исчисление как можно более простым языком, раскрыть его красоту и показать, что в каком-то смысле Вы уже знаете всё это, просто в других терминах.

Почему Вы бы хотели тратить свое время на чтение о такой сложной теме?

Потому что лямбда исчисление:

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

Плюс, это всегда хорошо — знать на чем основаны инструменты, которыми Вы пользуетесь, как они работают на фундаментальном уровне.

Не бойтесь сложных слов. Я объясню каждое из них, и обещаю, всё это не так сложно как кажется…


This article aims to describe lambda calculus as simple as possible, reveal its beauty and show that they’re already know all that in some way, just in other definitions.

Why would you want to spend your time reading about such a complex thing? Because lambda calculus:

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

Plus, it’s a good thing to know what the tools you’re using are based on, how do they work on the fundamental level.

Don’t be afraid of complex words. I’ll explain every one of them…


The point of this article is to show you how to write your code like a story that everyone can understand.

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.

Every point in this article is provided with visual example for ease of understanding.

This article will contain examples only in JavaScript, because it’s my main programming language, but the principles that I’ll describe here, can be used in any programming language you like.

Ok…

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