Comments (5)
Here's my first attempt at this. It produces the result atleast.
public IEnumerable<T> Interleave<T>(params IEnumerable<T>[] sequencesToInterleave)
{
var ls = sequencesToInterleave;
var lsl = ls.Length;
List<IEnumerator<T>> ies = new List<IEnumerator<T>>(lsl);
for (int i = 0; i < lsl; i++)
{
var en = ls[i].GetEnumerator();
ies.Add(en);
//en.MoveNext();
}
while (true)
{
for (int i = 0; i < lsl; i++)
{
var e=ies[i];
if (!e.MoveNext()) { e.Reset(); if (!e.MoveNext()) continue; }
yield return e.Current;
}
}
}
Original comment by [email protected]
on 9 May 2014 at 1:06
from morelinq.
This variation should stop the yielding once the longest enumerable reaches its
end... Not sure I'm happy with how it returns 6 of the non-numeric chars for
the test above while the longest enum has 5 numeric chars. I don't know how to
fix this yet.
public static IEnumerable<T> InterleaveForLongest<T>(params IEnumerable<T>[] sequencesToInterleave)
{
var ls = sequencesToInterleave;
var lsl = ls.Length;
bool[] resets = new bool[lsl];
List<IEnumerator<T>> ies = new List<IEnumerator<T>>(lsl);
for (int i = 0; i < lsl; i++) ies.Add(ls[i].GetEnumerator());
while (true)
{
for (int i = 0; i < lsl; i++)
{
var e = ies[i];
if (!e.MoveNext()) {
e.Reset(); resets[i] = true;
if (resets.All(x => x == true)) yield break;
if (!e.MoveNext()) continue;
}
yield return e.Current;
}
}
}
Original comment by [email protected]
on 9 May 2014 at 1:23
from morelinq.
Here's the final code I ended up with for now. I needed to group some data by
custom key and then interleave the values of each group...
var g = data.GroupBy(x => x.type);
foreach (var item in InterleaveForLongestG(g))
Console.WriteLine(item.ToString());
public static IEnumerable<T> InterleaveForLongestG<K, T>(IEnumerable<IGrouping<K, T>> sequencesToInterleave)
{
var ls = sequencesToInterleave.ToList();
var lsl = ls.Count;
bool[] resets = new bool[lsl];
List<IEnumerator<T>> ies = new List<IEnumerator<T>>(lsl);
for (int i = 0; i < lsl; i++) ies.Add(ls[i].ToList().GetEnumerator());
while (true)
{
for (int i = 0; i < lsl; i++)
{
var e = ies[i];
if (!e.MoveNext())
{
e.Reset(); resets[i] = true;
if (resets.All(x => x == true)) yield break;
if (!e.MoveNext()) continue;
}
yield return e.Current;
}
}
}
Original comment by [email protected]
on 9 May 2014 at 1:58
from morelinq.
Turns out I also needed a predicate for more control. Here's hopefully the
final things I ended up using.
public static IEnumerable<T> EnumInfiniteInterleaved<T>(Func<T, bool> predicate, params IEnumerable<T>[] sequencesContractoInterleave)
{
var ls = sequencesContractoInterleave;
var lsl = ls.Length;
if (lsl < 2) throw new ArgumentOutOfRangeException("Atleast two sequences required");
List<IEnumerator<T>> ies = new List<IEnumerator<T>>(lsl);
for (int i = 0; i < lsl; i++) ies.Add(ls[i].GetEnumerator());
while (true)
{
for (int i = 0; i < lsl; i++)
{
var e = ies[i];
nxt:
if (!e.MoveNext()) { e.Reset(); if (!e.MoveNext()) continue; }
if (!predicate(e.Current)) goto nxt;
yield return e.Current;
}
}
}
public static IEnumerable<T> EnumInfinite<T>(Func<T, bool> predicate, IEnumerable<T> source)
{
if (source == null) yield break; // be a gentleman
IEnumerator<T> e = source.GetEnumerator();
iterateAllAndBackToStart:
while (e.MoveNext())
{
var c = e.Current;
if (predicate(e.Current)) yield return c;
}
e.Reset();
goto iterateAllAndBackToStart;
}
Original comment by [email protected]
on 9 May 2014 at 1:40
from morelinq.
This issue has been migrated to:
https://github.com/MoreLINQ/morelinq/issues/89
The conversation continues there.
DO NOT post any further comments to the issue tracker on Google Code as it is
shutting down.
You can also just subscribe to the issue on GitHub to receive notifications of
any further development.
Original comment by azizatif
on 21 Aug 2015 at 6:56
from morelinq.
Related Issues (20)
- add Memoize support HOT 2
- ForEach should optionally take an index HOT 2
- ToDataTable throws on type with public static member HOT 6
- It would be nice to have a Top operator that would merge OrderBy and Take HOT 1
- F# countBy analogue HOT 2
- Usage example for SingleOrFallback HOT 7
- converting IEnumerable<TSource> into IEnumerable<IEnumerable<TSource>>? HOT 2
- Buffering HOT 2
- Please make .NET 4 compatible distribution (or allow silent modifications) HOT 1
- TakeLast example incorrect HOT 5
- Patch for /MoreLinq/DistinctBy.cs HOT 2
- Implement Full Outer Join HOT 1
- ExpandoObject inclusion HOT 4
- Patch for /MoreLinq/EquiZip.cs HOT 1
- Change ArgumentNullException to early return. HOT 1
- MaxByOrDefault HOT 3
- Scan should not throw on empty sequences HOT 1
- Batch should return an IEnumerable<IList<T>> HOT 2
- Migrate to GitHub/BitBucket HOT 15
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.