Created
January 4, 2012 14:32
-
-
Save masaru-b-cl/1560284 to your computer and use it in GitHub Desktop.
MapBetween
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; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace ConsoleApplication1 | |
{ | |
public static class EnumerableEx | |
{ | |
public static IEnumerable<TResult> MapBetween<TValue, TResult>(this IEnumerable<TValue> source, Func<TValue, TValue, TResult> f) | |
{ | |
return from a in source.Select((x, i) => new { X = x, Index = i }) | |
join b in source.Skip(1).Select((x, i) => new { X = x, Index = i }) on a.Index equals b.Index | |
select f(a.X, b.X); | |
} | |
} | |
public class Program | |
{ | |
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