Skip to content

Instantly share code, notes, and snippets.

@miya2000
Created June 10, 2011 02:55
Show Gist options
  • Save miya2000/1018163 to your computer and use it in GitHub Desktop.
Save miya2000/1018163 to your computer and use it in GitHub Desktop.
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