Comments (9)
ah, I had forgotten this :)
What I want can be implemented on top of AggregateAdjacent
, if that's what you mean:
IEnumerable<IGrouping<G, E>> GroupByAdjacent<T, K, G, E>(
this IEnumerable<T> source,
Func<T, K> key,
Func<T, G> group,
Func<T, E> element)
{
return source.AggregateAdjacent(
key,
(k, x) => new Grouping(group(x)),
(k, g, x) =>
{
g.Add(element(x));
return g;
},
(k, g) => g
);
}
from morelinq.
Sorry, but I am having trouble to understand the problem and what you're asking for.
If this is the input:
aKey
a1
a2
a3
b1
b2
b3
What do you expect the output to be?
from morelinq.
Basically, I added this to my project. It's a slightly less efficient way of doing Proposed Solution 1:
IEnumerable<IGrouping<G, E>> GroupAdjacent<T, K, G, E>(
this IEnumerable<T> source,
Func<T, K> key,
Func<T, G> group,
Func<T, E> element)
{
K cachedKey = default;
G cachedGroup = default;
return source.GroupAdjacent(
x =>
{
var nextKey = key(x);
if (nextKey != cachedKey || cachedGroup is null)
(cachedKey, cachedGroup) = (nextKey, group(x));
return cachedGroup;
},
element
}
The key point is that I don't want to create a G
instance for every item in the source enumeration, only once per group.
from morelinq.
Thanks for sharing the code. It helps to understand the mechanics a bit. So if the grouped elements were exposed as a list instead of a sequence, like your proposed solution 2, then it would solve your problem?
GroupAdjacent
signatures were modeled after GroupBy
, which is why the group elements are typed as IEnumerable<>
.
from morelinq.
Yes, if GroupAdjacent
exposed IList
we could efficiently build the groups by peeking elements[0]
. (Proposed Solution 2).
I like Proposed Solution 1 a little more because it's simpler on consumer side. With Solution 2 consumer has to take care of the IGrouping construction.
Independently of Solution 1 or 2: if buffering is not an implementation detail that will change, MoreLinq might as well expose IList
. It opens up more flexibility to efficiently consume GroupAdjacent
results on the consumer side.
I think it's fair to commit to buffering, because streaming is a valuable alternative but it comes with strict constraints on the way you consume it.
For instance, you can't enumerate one group elements anymore after you've moved to the next group. And you can only enumerate once.
For these reasons, a non-buffering implementation would IMHO have to be offered by a new API.
from morelinq.
Thanks for the clarifications.
from morelinq.
@jods4 Yes, that's it!
from morelinq.
What about the following with the existing api:
seq.GroupAdjacent(
x => (x.aKey, x.a1, x.a2, x.a3),
x => new B(x.b1, x.b2, x.b3),
(k, g) => (new A(k.aKey, k.a1, k.a2, k.a3), g));
Unless a1
, a2
, or a3
are non-comparable, this will work fine.
from morelinq.
@viceroypenguin
Minor point: I have more a_n
properties than I want to copy each iteration (ValueTuple is a struct)...
Major point: MoreLinq would then compare all a_n
(default struct comparison) for each element, I want to compare aKey
only.
from morelinq.
Related Issues (20)
- FullGroupJoin with null keys HOT 1
- Suggestion: WithIndex<T> Extension Method to facilitate enumerating with an item and index tuple HOT 1
- Compile Error MoreLinq/ToDataTable.cs HOT 2
- Hide `ToDictionary()` in net8+
- Remove `Windowed` that's been obsolete since 3.0
- Add `Minima` & `Maxima` to supersede `MinBy` & `MaxBy`
- Hide `CountBy` in net9+
- Review NUnit1030
- Review NUnit1029
- `Pad` should throw `ArgumentOutOfRangeException` for invalid width
- `PadStart` should to throw `ArgumentOutOfRangeException` for invalid width
- `Interleave` shouldn't validate `otherSequences` members
- Remove tail items while condition is true
- Revise tests to avoid using `Eumerable.Range` HOT 4
- `Subsets` enumerator doesn't return same array from `Current`
- `Merge` masks original exception with an `ArgumentException` about no tasks
- Async versions of OrderedMerge and GroupAdjacent HOT 3
- `Rank` does not iterate source on re-iterations
- Installing MoreLINQ NuGet 4.2 "Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))" HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from morelinq.