Curry Functions One Argument at a Time

Every step in the last lesson’s pipes took exactly one argument. However, Marco’s bonuses don’t fit that shape. The Music round earns a bonus of three points per team, and the Picture round earns two. A bonus step needs two values, the points and the bonus, so it can’t sit in a pipe as it is.

In this lesson, you’ll write a function that takes its arguments one call at a time, read it arrow by arrow, and use it to make one bonus function per round. You’ll also see when this style isn’t worth it.

Count the Arrows

A function that needs two values can wait for the second one if it takes the first and returns a function for the rest. That’s the idea behind currying: you turn a function with several parameters into a chain of functions that each take one. Here’s Marco’s bonus as a curried function:

Language: JavaScript Filename: bonus.js
const addBonus = (bonus) => (points) => points + bonus;

const musicBonus = addBonus(3);
console.log(musicBonus(7));
console.log(addBonus(2)(8));
console.log(addBonus(3, 7));

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.