Lets assume you have a function that returns a lazily-enumerated object:
struct AnimalCount
{
int Chickens;
int Goats;
}
IEnumerable<AnimalCount> FarmsInEachPen()
{
....
yield new AnimalCount(x, y);
....
}
You also have two functions that consume two separate IEnumerable
s, for example:
ConsumeChicken(IEnumerable<int>);
ConsumeGoat(IEnumerable<int>);
How can you call ConsumeChicken
and ConsumeGoat
without a) converting FarmsInEachPen()
ToList() beforehand because it might have two zillion records, b) no multi-threading.
Basically:
ConsumeChicken(FarmsInEachPen().Select(x => x.Chickens));
ConsumeGoats(FarmsInEachPen().Select(x => x.Goats));
But without forcing the double enumeration.
I can solve it with multithread, but it gets unnecessarily complicated with a buffer queue for each list.
So I'm looking for a way to split the AnimalCount
enumerator into two int
enumerators without fully evaluating AnimalCount
. There is no problem running ConsumeGoat
and ConsumeChicken
together in lock-step.
I can feel the solution just out of my grasp but I'm not quite there. I'm thinking along the lines of a helper function that returns an IEnumerable
being fed into ConsumeChicken
and each time the iterator is used, it internally calls ConsumeGoat
, thus executing the two functions in lock-step. Except, of course, I don't want to call ConsumeGoat
more than once..
AnimalCount
toIEnumerable<AnimalCounts>
toIEnumerable<int>
(I presume? can't even tell your intent...). As this stands now is getting flagged as not a real question. – CopybookAnimalCount
in the first place. – Affection