Jonathan Boccara's blog

How to Send an STL Collection to a Curried Object

Published May 10, 2019 - 0 Comments

STL intersperse

After seeing how to send individual objects to a curried object, let’s see how we can haul a whole collection into one of those curried creatures. One use case for this is to intersperse a collection of strings with commas.

If you’re jumping in the topic of curried objects just now, a curried object is an intermediary object that facilitates the dialogue between a caller and a callee by fixing some parameters and potentially adapting other parameters.

We’ve seen that those objects can carry some logic that ends up simplifying the application code. If you want to catch up on the previous episodes, the series on curried objects contains:

  • Part 1: Constant curried objects
  • Part 2: How to Output Strings Separated by Commas in C++ (Mutable curried objects)
  • Part 3: Sending an STL Collection to a Curried Object

Motivating example

Let’s pick up our curried object CSVPrinter. It accepts successive strings and sends them to a stream by alternating them with commas, and makes sure not to write a trailing comma at the end of the stream.

Here is the implementation of CSVPrinter:

Note that this is just one particular case of curried object. Curried object don’t have to have an operator<<.

Here is some calling code to exerce it:

This code outputs:

Now let’s take a collection of strings:

And let’s send all the objects of this collection to our curried object. In our case, it will print them by interspersing them with commas:

Indeed, the following code outputs:

Now we have a very simple operation, that has a very simple code and that’s all well.

So what the point of going further?

It is to integrate curried objects with STL algorithms, to let the algorithms send their outputs into a curried object.

Not that it would be useful in this particular case, because the code is so simple here. But working on such a basic case will let us focus on the integration of the curried object with STL algorithms in general (to easily intersperse their outputs with commas, for one example).

So let’s get into this.

First (bad) attempt: using a function object

To turn this piece of code into an algorithm call:

A intuitive option could be to use std::for_each:

Granted, this may not be an improvement to the code because it was so simple, but we’re just studying how to connect an algorithm with a curried object in a simple case.

Let’s run the code:

The result is correct. But is this the right way to integrate the curried object with the STL? Can we generalize it to other algorithms than for_each?

The answer is No, for at least two reasons. One is that all algorithms don’t take a function object, to begin with. Take set_difference, or partial_sum, or rotate_copy for example.

The other reason is that even for the algorithms that do take a function object, such as std::transform for instance, some don’t guarantee that they will traverse the input range in order. So the algorithm may call the function object in any order and send the result to our curried object in an order different from the input, that could lead to, for example:

std::for_each guarantees to traverse the input collection in order tough.

Note that in general, carrying a mutable state inside of a function object can lead to incorrect results because most algorithms are allowed to make internal copies of the function object (std::for_each guarantees that it won’t, though). This leads to the mutable state being located in different object, that could lose consistency with each other (this is why in the STL function objects, stateless is stressless). However, here we don’t have this problem since the function object only has a reference to the state, and not the state itself.

Anyway, for the above two reasons using a function object to connect an STL algorithm to a curried object is not a good idea.

So what to do then?

A better solution: using the output iterator

Going back to our initial code:

Another way to see the situation is that we’re sending data to the CSVPrinter, or said differently, that we’re copying data from the sentence container over to the CSVPrinter. So instead of std::for_each, we could use std::copy.

But then, we need something to make std::copy send the data to the curried object. std::copy uses an output iterator to emit its output data. So we need an custom output iterator that we could customize and plug to CSVPrinter.

A custom inserter? Let’s use custom_inserter!

As a reminder, the definition of custom_inserter looked like this:

The most important part in custom_inserter is this:

It is an iterator that, when an algorithm sends data to it, passes on this data to a custom function (insertFunction_ in the above code).

Here is how custom_inserter can help us connect std::copy to our curried object CSVPrinter:

which outputs:

We had encountered custom_inserter when making STL algorithms output to legacy collections, but we see here another usage: outputting to a curried object.

In a more elaborate operation on a collection, such as std::set_difference for example, we can use custom_inserter to send the output of the algorithm to the curried object in a similar way:

Using the channel of the output iterators doesn’t suffer from the two issues that we raised when attempting to use the function object. Indeed:

  • all the algorithms that output a range do have one (or more) output iterators,
  • even if some algorithms don’t treat the input in order, they all send data to the output in order.

It’s not just about interspersing with commas

All the way through this example, we’ve used a curried object to intersperse the words of a sentence with commas. Note that this curried object wrapped into an output iterators is in the technical specification for the future standard library under the name of std::ostream_joiner. But this is just one specific case of curried objects, and there are other ways than curried objects to fill this need.

As a side note, the most elegant way I know of to intersperse a sentence with commas is by using the range-v3 library:

Which outputs:

Isn’t it beautiful? However if you don’t have range-v3 available, a curried object is a nice way to do the job, in my opinion.

Conversely, curried objects can be used for so much more. They make application code (and therefore, life) easier to read and write and, as an icing on the cake they can be integrated with the STL by using smart output iterators.

That’s it for our series on curried objects. Your reactions are, as usual, welcome.

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.