
John Torjo wrote:
Neal D. Becker wrote:
Thanks for the advice. I think I have it now.
Problem:
A function F accepts a pair of stl-style iterators as input. A nullary functor G can generate an output. Arrange for F to call G n times without storing to an intermediate sequence.
Example: G is a random number generator.
I wonder if there is a more elegant approach? Here is what I came up with.
Yes, there is. rtl (Range Templates Library) has a generate_iterator.
You have a generator, and a stopper. From these, you create an input iterator sequence. The stopper tells you when you've reached end.
For instance: // input iterator sequence - 20 fibonacci numbers generated(fibonacci(), gen_n(20))
// input iterator sequence - fibonacci numbers up to 200 generated(fibonacci(), gen_upto(200))
You can use it like this: rng::copy( generated(fibonacci(), gen_upto(200)), std::ostream_iterator<int>(std::cout," "));
The rtl (and the generate* examples) can be found at: www.torjo.com/code/for_boost.zip
The RTL approach looks very interesting. There is a lot to digest here. What is the state of maturity of this code? Would you consider it ready for use?