Created
April 4, 2014 18:39
-
-
Save kamiyaowl/9980702 to your computer and use it in GitHub Desktop.
c# IEnumerable lazy eval
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
1st | |
<<< InfRandomArr Called >>> | |
<<< InfRandomArr Called >>> | |
<<< InfRandomArr Called >>> | |
0.574436781264114 0.135079779259432 0.720461940728343 | |
2nd | |
<<< InfRandomArr Called >>> | |
<<< InfRandomArr Called >>> | |
<<< InfRandomArr Called >>> | |
0.431247790079214 0.772239840017743 0.203060114850784 | |
3rd | |
<<< InfRandomArr Called >>> | |
0.140674664238782 |
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; | |
static class Extension { | |
public static Random Random = new Random(); | |
public static IEnumerable<double> InfRandomArr() { | |
while (true) { | |
Console.WriteLine("<<< InfRandomArr Called >>>"); | |
yield return Random.NextDouble(); | |
} | |
} | |
public static void ForEach<T>(this IEnumerable<T> src, Action<T> f) { | |
foreach (var s in src) | |
f(s); | |
} | |
} | |
class EnumerableTest { | |
static void Main(){ | |
var hoge = Extension.InfRandomArr().Take(3); | |
Console.WriteLine("1st"); | |
hoge.ToArray().ForEach(x => Console.Write("{0}\t",x)); | |
Console.WriteLine("\n2nd"); | |
hoge.ToArray().ForEach(x => Console.Write("{0}\t",x)); | |
Console.WriteLine("\n3rd"); | |
hoge.Take(1).ForEach(x => Console.Write("{0}\t",x); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment