forked from elastic/elasticsearch-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumerableExtensions.cs
40 lines (34 loc) · 924 Bytes
/
EnumerableExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System.Collections.Generic;
using System.Linq;
namespace DocGenerator
{
public static class EnumerableExtensions
{
//lazy programmer:
// http://stackoverflow.com/questions/23222046/combine-entries-from-two-lists-by-position-using-linq
public static IEnumerable<T> Intertwine<T>(this IEnumerable<T> one, IEnumerable<T> two, bool swap = false)
{
if (swap)
{
var tmp = one;
one = two;
two = tmp;
}
using (IEnumerator<T> enumeratorOne = one.GetEnumerator(),
enumeratorTwo = two.GetEnumerator())
{
bool twoFinished = false;
while (enumeratorOne.MoveNext())
{
yield return enumeratorOne.Current;
if (!twoFinished && enumeratorTwo.MoveNext())
yield return enumeratorTwo.Current;
else twoFinished = true;
}
if (twoFinished) yield break;
while (enumeratorTwo.MoveNext())
yield return enumeratorTwo.Current;
}
}
}
}