Created
June 10, 2011 02:55
-
-
Save miya2000/1018163 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Text; | |
using System.Diagnostics; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var array = new[] { "1", "aaa", "2", "bbb", "1", "ccc", "4" }; | |
foreach (var i in array.ZipNext().ToDictionary()) | |
{ | |
Debug.WriteLine(i); | |
} | |
} | |
} | |
static class Ex | |
{ | |
public static IEnumerable<Tuple<T, T>> ZipNext<T>(this IEnumerable<T> source) | |
{ | |
using (var e = source.GetEnumerator()) | |
{ | |
while (e.MoveNext()) | |
{ | |
var item = e.Current; | |
if (e.MoveNext()) | |
{ | |
yield return Tuple.Create(item, e.Current); | |
} | |
} | |
} | |
} | |
public static Dictionary<T, T> ToDictionary<T>(this IEnumerable<Tuple<T, T>> source) | |
{ | |
return source.ToLookup(t => t.Item1).ToDictionary(g => g.Key, g => g.Last().Item2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment