cpplinq icon indicating copy to clipboard operation
cpplinq copied to clipboard

Iterating results without to_vector()

Open malamanteau opened this issue 9 years ago • 3 comments

I was wondering, how can I iterate over query results without putting them into a vector first?

So for example, if I do this:

for (auto const & item : (cpplinq::from(ins) >> cpplinq::distinct() >> cpplinq::to_vector()))
{
	//stuff
}

I would rather iterate over each item in-place, without making a copy, is there a way to do this?

malamanteau avatar Dec 21 '16 21:12 malamanteau

@vectorantics I'm pretty sure from() and distinct() are both just cpplinq constructs, so, yes, AFAIK, you need to vector-ize it.

mwpowellhtx avatar Dec 21 '16 22:12 mwpowellhtx

You can do something like:

GeneralResult generalResult;
cpplinq::from(calculators_)
	>> cpplinq::orderby([](const IResultCalculator &x) {return x.Priority; })
	>> cpplinq::for_each([&](IResultCalculator &x)
{
	x.CalculateResult(data, generalResult);
});

This function in the for_each will work on references to the original objects instead of copies. But then a question from my side: Is there a way to conditionally break out of this loop like the break keyword for normal loops? This solution is not very elegant:

GeneralResult generalResult;
bool brk = false;
cpplinq::from(calculators_)
	>> cpplinq::orderby([](const IResultCalculator &x) {return x.Priority; })
	>> cpplinq::for_each([&](IResultCalculator &x)
{
	if (brk)
	{
		return;
	}
	x.CalculateResult(data, generalResult);
	if (something)
	{
		brk = true;
	}
});

Stannieman avatar Aug 04 '17 07:08 Stannieman

The preferred way would be for (auto&& x : ... >> experimental::container())

KN4CK3R avatar Oct 02 '17 20:10 KN4CK3R