'How to lazily partition an IAsyncEnumerable?

I have an IAsyncEnumerable that returns what is essentially a sequence of Key/IEnumerable<Value> pairs. I have code consuming this and other similar enumerables, that assumes it will be receiving a unique collection of keys. But one of my data sources does not obey this constraint. It does, however, keep duplicate keys grouped together. (You won't see [k1, k2, k1].)

This should be fairly simple to resolve with a wrapper that partitions the data by key and concatenates the values, except that I don't see any usable partitioning operator in System.Linq.Async. There are GroupBy and ToLookup, but both of these are eager operators that will consume the entire sequence immediately. This is not suitable for my purposes, due to large amounts of data being involved.

Is there any simple way to partition an IAsyncEnumerable similar to GroupBy, grouping inputs according to a key selector, but keeping its behavior fully lazy and generating new groupings on demand when the key changes?

EDIT: I looked to see if MoreLINQ has anything like this, and found GroupAdjacent, but the code shows that, while it does not eagerly consume the entire input sequence, it will still eagerly consume the entire group when starting a new group. I'm looking for a method that will return a lazy enumerable in its groupings. It's trickier than it sounds!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source