views:

116

answers:

1

Behold the C# code:

 IEnumerable<int> innerMethod(int parameter)
 {
     foreach(var i in Enumerable.Range(0, parameter))
     {
         yield return i;
     }
 }

 IEnumerable<int> outerMethod(int parameter)
 {
     foreach(var i in Enumerable.Range(1, parameter))
     {
         foreach(var j in innerMethod(i))
         {
              yield return j;
         }
     }
 }

The question is: There is a way for outerMethod have the same output without iterating over innerMethod output?

+4  A: 

Unfortunately not.

In F# you could do something like

yield! innerMethod(i)

but there's no equivalent in C#.

I mean, in this particular case you could replace the method with:

 IEnumerable<int> outerMethod(int parameter)
 {
     return Enumerable.Range(1, parameter)
                      .SelectMany(x => innerMethod(x));
 }

but I expect you wanted a more general purpose way of doing it. (If that helps though, great!)

Jon Skeet
did you edit in the SelectMany? I'm sure I didn't see it when I replied...
Marc Gravell
Yup, although pretty quickly...
Jon Skeet
Is that supposed to be `1.parameter`, or `1, parameter` Jon?
Matthew Scharley
Oops, "1, parameter" as per question. Will edit.
Jon Skeet
Hmm... mister Skeet is starting to make mistakes... this cannot be a good sign.
Peter Lillevold