XDracam / unity-corelibrary

Collection of classes and extension methods that make life with Unity3D more comfortable
MIT License
16 stars 3 forks source link

Pairwise Enumerable Extensions #38

Open Eregerog opened 5 years ago

Eregerog commented 5 years ago

These extension methods would be similar to existing ones, but provide the element and the previous element as parameters of the function. This should apply to the following methods: ForEachPairwise<T>(Action<T,T> action) AnyPairwise<T>(Func<T,T,bool> predicate) SelectPairwise<T,TRes>(Func<T,T,TRes> selector) WherePairwise<T>(Func<T,T,bool> predicate) and perhaps other extension methods

XDracam commented 5 years ago

Is this a sliding or grouping operation?

Sliding: [0, 1], [1, 2], [2, 3] Grouping: [0, 1], [2, 3]

What we could provide instead is this feature:

IEnumerable<IEnumerable<A>> Grouped<A>(this IEnumerable<A> enumerable, uint groupSize)
IEnumerable<IEnumerable<A>> Sliding<A>(this IEnumerable<A> enumerable, uint slideSize)

These would allow you to do all of your use cases in a generic fashion. What we could then do is following C#7+ only feature:

IEnumerable<(A, A)> PairwiseGrouped<A>(this IEnumerable<A> enumerable, uint groupSize)
IEnumerable<(A, A)> PairwiseSliding<A>(this IEnumerable<A> enumerable, uint slideSize)

Another convenience feature for C#7 which could follow is tuple-deconstructing LINQ methods, which would address the problem of missing deconstruction in function parameter declaration:

IEnumerable<C> Select<A, B, C>(this IEnumerable<(A, B)>, Func<A, B, C> mapping)
IEnumerable<(A, B)> Where<A, B>(this IEnumerable<(A, B)>, Func<A, B, bool> predicate)
// etc pp, maybe for 3-tuple as well

The last one would need an additional issue

Eregerog commented 5 years ago

The Grouping and Sliding sounds like a generic way of what i wanted. The pairwise grouping/sliding also sounds good