Skip to content

Instantly share code, notes, and snippets.

@masaru-b-cl
Created January 4, 2012 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masaru-b-cl/1560454 to your computer and use it in GitHub Desktop.
Save masaru-b-cl/1560454 to your computer and use it in GitHub Desktop.
MapBetween (on Zip Method)
using System;
using System.Linq;
using System.Collections.Generic;
namespace ConsoleApplication1
{
public static class EnumerableEx
{
public static IEnumerable<TResult> MapBetween<TValue, TResult>(this IEnumerable<TValue> source, Func<TValue, TValue, TResult> f)
{
return source
.Zip(
source.Skip(1),
(x, y) => new { OldValue = x, NewValue = y })
.Select(x => f(x.OldValue, x.NewValue));
}
}
public class Program
{
public static void Main(string[] args)
{
var q = new[] { 1, 2, 3, 4, 5 }.MapBetween((a, b) => a + b);
foreach (var n in q)
{
Console.WriteLine(n);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment