Jonathan Boccara's blog

Curried Objects in C++

Published May 3, 2019 - 0 Comments

Curried objects are like facilitators. They consist in intermediary objects between a caller and a callee, and helps them talk to each other in a smooth way. This ability makes the code simpler and easier to read.

While having seen and used the pattern at various places, the first time I encountered the actual term “Curried object” was in an article from James Noble, which clarified the bigger picture about those friendly little creatures.

An typical example of usage for curried objects is when outputting a line of strings separated by commas. If you’ve ever tried it, you probably encountered the obnoxious problem of the last word that should not be followed by a comma, and that forces us to write annoying bookkeeping code to check whether or not to print the bloody comma.

As we’ll see, curried object can relieve your code from those concerns. But this involves mutable curried objects, which we tackle in Part 2 of the series.

There are other uses for curried objects too, and for now we focus on constant curried objects.

Indeed, this series on curried objects contains:

  • Curried objects – Part 1: Constant curried objects
  • Curried objects – Part 2: Mutable curried objects
  • Curried objects – Part 3: Curried objects and the STL

We’ll begin with a simple example and gradually build more elaborate ones. Let’s get more into the details of those little beings that want to make our lives easier.

Constant curried objects

Curried objects are closely related to functions. In fact, the word “currying” essentially means partial application of a function.

What does that mean in practice?

Imagine that we have a function that takes several (or even too many) parameters, and that you need to call that function multiple times by making only a limited number parameters vary every time.

For instance, consider this function that draws a point at coordinates x and y, and z:

For the sake of the example, this function only prints out the points coordinates. To simplify the graphics generation in the examples that follow, I will feed the program outputs into MS Excel and generate the associated chart.

Factorizing a common parameter

Let’s try out this function to draw each of the four cardinal points in the plane at z=0. We could write:

But the last parameter doesn’t bring any information when reading code here. Indeed, we only work in a plane at z=0, so we think in terms of x and y only.

We can therefore partially apply drawPoint by fixing the last argument at 0, which would result into a function that only takes x and y as parameters. This is called currying, but in practice we can implement it with a familiar lambda:

No more third coordinate to read about here.

Here is the code outputs:

And the corresponding chart:


Adapting parameters

Not convinced it’s worth it? Let’s see a slightly more complex example that does not only make a partial application, but also makes an adaptation of parameters (so strictly speaking, this is not only “currying” then).

We now want to draw a line of points identified by a slope and an y-intercept. We can refine our curried object to take a slope and a y-intercept and draw a point on this line, given an abscissa x:

Note that this code uses C++14’s auto return type in order to write expressive code with lambdas, but the lambda could be written in C++11 without the intermediary function drawOnLine. Or even with a functor in C++98. Those are various ways of writing our curried objects, but the idea remains the same: it is an object that facilitates the dialogue between the caller (here, main()) and the callee (here drawAt).

Here is the generated output:

And the corresponding graphic:


Let’s now take a more elaborate example: let’s draw a circle!

We now have a drawInPlane method that takes an abscissa x and an ordinate y, and draws a point at that position. But those cartesian coordinates are just one way to identify a position in a plane.

Another representation of the plane is via polar coordinates: a distance r from an origin and an angle theta with the horizontal axis. To draw a circle for example, it is way easier to use polar coordinates than cartesian coordinates.

The curried object that we will create will adapt polar coordinates to cartesian coordinates with the following mathematical formulae:

curried object

curried object

Let’s now create our curried object that will take a succession of angles and draw a point on the circle for each of those angles:

Let’s now use the curried object to generate some points on the circle:

As a side note, you may have noticed this particular example is in dry need of strong typing, to be able to write something like that:

But end of side note, let’s keep the focus on curried objects.

Here is the output of the program:

And here is the corresponding graphic:


Isn’t it too much indirection?

Let’s have a look at the code to generate those points, all put together:

Now let’s compare it with an equivalent code, but that doesn’t use any curried object:

The version with curried objects has more lines of code, and more indirections. Is it a good thing or a bad thing?

By itself, having more lines of code is not a good thing. But to decide if curried objects are worth this investment, let’s consider what they brought us:

  • more labels: if you had first seen the second version of the code above, the one without curried objects, would you have guessed it was drawing a circle? You probably would have, but after how much time? The version with curried objects has more code, but the extra lines carry information about the intent of the code. For this reason, I think they are useful.
  • more reuse: if we want to draw another circle, the function drawOnCircle is there to be reused. And if we have several circles to draw, the version with curried objects will end up having less lines of code. More importantly, this version removes some code duplication that the one without curried objects will have if we multiply the circles.

Now I’d be interested to hear you opinion of this. Are curried objects worth it in your opinion?

What is constant in Constant curried objects

You will notice that all those curried objects, that we have implemented as lambdas, have an operator() that is const (this is the default behaviour of lambdas). They all contain data, but this data is not modified by the application of the curried object.

What happens when the state of the curried object is modifiable? Does it bring any benefit?

It turns out that it does, and this is what we explore in Part 2 of the series on curried objects in C++.

Related articles:

Become a Patron!
Share this post! Facebooktwittergoogle_pluslinkedin    Don't want to miss out ? Follow:   twitterlinkedinrss

Get a free ebook on C++ smart pointers

Get a free ebook of more than 50 pages that will teach you the basic, medium and advanced aspects of C++ smart pointers, by subscribing to our mailing list! On top of that, you will also receive regular updates to make your code more expressive.

No spam. You can unsubscribe at any time.