Created
January 4, 2012 15:11
-
-
Save masaru-b-cl/1560454 to your computer and use it in GitHub Desktop.
MapBetween (on Zip Method)
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.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