Last active
August 29, 2015 14:14
-
-
Save kamiyaowl/4925a70f8aaa40dc7b8b to your computer and use it in GitHub Desktop.
linqとか比較しながらselectとか
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; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication3 { | |
class Program { | |
static void Main(string[] args) { | |
var src = LinqExtension.RandomSource(10).Take(10).ToArray(); | |
src.Print(); | |
src.CompareSelect((x, y) => x < y).Print(); | |
//[7,4,8,6,7,4,5,9,4,9,] | |
//[False,True,False,True,False,True,True,False,True,] | |
Console.Read(); | |
} | |
} | |
static class LinqExtension { | |
public static IEnumerable<int> RandomSource(int max = int.MaxValue) { | |
var r = new Random(); | |
while (true) { | |
yield return r.Next(max); | |
} | |
} | |
public static IEnumerable<U> CompareSelect<T, U>(this IEnumerable<T> src, Func<T, T, U> selector) { | |
return src.Zip(src.Skip(1), (x1, x2) => new { X1 = x1, X2 = x2 }).Select(x => selector(x.X1, x.X2)); | |
} | |
public static IEnumerable<T> Print<T>(this IEnumerable<T> src) { | |
Console.Write("["); | |
foreach (var item in src) { | |
Console.Write("{0},", item); | |
} | |
Console.WriteLine("]"); | |
return src; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment