I noticed that generate_n(foo, n) calls foo n times while generate(foo) | take(n) calls it n+1 times. Is this by design? If so, what is the rationale?
In some situations I would like to guarantee that foo is called exactly n times, but it seems that I can't always use generate_n. For example, say that I want to do something like this:
for_each(generate(foo), [](auto x) { return yield_if(bar(x), x); }) | take(n);
In this case, foo might be called more than n times depending on how often bar(x) returns false, but I would like to ensure that it is called exactly as many times as it takes to generate n elements and no more. I don't see how I could do it with generate_n. I guess I would need something like for_each_n, which (as far as I can see) only exists as an algorithm and not as a view.
I noticed that
generate_n(foo, n)callsfoontimes whilegenerate(foo) | take(n)calls itn+1times. Is this by design? If so, what is the rationale?In some situations I would like to guarantee that
foois called exactlyntimes, but it seems that I can't always usegenerate_n. For example, say that I want to do something like this:for_each(generate(foo), [](auto x) { return yield_if(bar(x), x); }) | take(n);In this case,
foomight be called more thanntimes depending on how oftenbar(x)returnsfalse, but I would like to ensure that it is called exactly as many times as it takes to generatenelements and no more. I don't see how I could do it withgenerate_n. I guess I would need something likefor_each_n, which (as far as I can see) only exists as an algorithm and not as a view.