Let's say I want a sequence of 10 numbers, and I have a function which produces numbers on demand:
var s = new List<int>();
for (var i=0; i<10; i++) {
s.Add(Magically_generate_a_very_special_number());
}
Is the usual way of accomplishing this. However, let's say I want to use LINQ. I can already do (let's ignore the distinction between types):
var s = Enumerable.Range(0, 10).Select(i => MathNet.Numerics.Statistics.DescriptiveStatistics())
Which is almost good enough for me. However, it bothers me that I need to specify a range of integers first - this is a superfluous step since I discard the value anyway. Is it possible to do something like the following?
var s = Enumerable.ObjectMaker(Magically_generate_a_very_special_number).Take(10);
It seems that Enumerable.Repeat almost does what I want, but it takes the result of the function I give it and then duplicates that, instead of repeatedly evaluating the function.
By the way, the inspiration for this question was the Math.Net IContinuousDistribution.Samples method. Its body looks like the following:
while (true)
{
yield return Generate_a_random_number();
}
So that you can obtain a sequence of samples from the distribution with myDistribution.Samples.Take. In principle, I could just write my own method to produce an iterator in the same way, but I'm wondering if there is one that already exists.