I'm learning C++ after having worked a lot with Haskell and functional languages in general, and I found that I'm constantly trying to solve the same problem:
- Read some data from an input stream
- Tokenize them based on a specific algorithm
- Process the tokens
If this was Haskell, I could simply take advantage of the fact that everything is lazy, and write my transformation as I think about it, and then it would get applied as the downstream is being consumed. There are even libraries that do this exact pattern (conduit and pipes).
Let's say that I wanted to take the sequence 1 2 3 4 5 6 ...
and output 12 34 56 ...
. I can see how to write ad-hoc code which operates on the stream and processes the data in place. But I'd like to know if there is an abstraction mechanism that allows me to build a new stream by transforming data (in any thinkable way) from another stream. This abstraction should allow me to buffer the data as I'm processing it, not just a simple mapping of a single element to a new value.
Here are the limitations:
- I can't use any other library other than the stdlib.
- It has to work on C++03 (meaning no C++11 features.)
If you're thinking, is this a homework? Well sort of, I'm getting a lot of class assignments which require me to work with streams of data (which is the reason for the no library and C++03 restrictions). It's not that I don't know how to do this using while
loops, but I'd like to know if there is an existing stream abstraction in the stl, just waiting to be discovered and used.
But if the only way to do this is to use C++11, then I'd like to know.