Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
c# IEnumerable lazy eval
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
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
You can’t perform that action at this time.