Skip to content

Instantly share code, notes, and snippets.

@masaru-b-cl
Created January 4, 2012 14:32
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/1560284 to your computer and use it in GitHub Desktop.
Save masaru-b-cl/1560284 to your computer and use it in GitHub Desktop.
MapBetween
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