none
return int sequences with step RRS feed

  • Question

  • very easy to write this:

    IEnumerable<int> GetIntegers(int from, int to, int step)
    {
    for( int i = from ; i <= to; i+= step)
    yield return i;
    }

    I wonder why microsoft doesn't provide a step function since they have Enumerable.Range(int start, int count), or is there one that I am not aware of?
    or someone can give me a better way to generate this kind of sequence values?
    Friday, August 7, 2009 5:26 PM

Answers

  • List<Int32> list = Enumerable.Range(0,21).ToList();
    for (int i = 0; i <= list.Count; i += 2)
    {
       .....
    }

    Note: I am not in front of VS at the moment, so if there any typo's I apologize
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Proposed as answer by Harry Zhu Friday, August 14, 2009 7:00 AM
    • Marked as answer by Harry Zhu Tuesday, August 18, 2009 8:46 AM
    Friday, August 7, 2009 5:49 PM
  •             var bytwos = Enumerable.Range(0, 21).Where(value => (value % 2) == 0);

    I have to say, there's no elegance in this kind of code.
    Hans Passant.
    • Proposed as answer by Harry Zhu Friday, August 14, 2009 7:00 AM
    • Marked as answer by Harry Zhu Tuesday, August 18, 2009 8:46 AM
    Friday, August 7, 2009 6:55 PM
    Moderator
  • I think that what is important here (for aguments sake) is that Microsoft can't implement every simple feature that they can think of. The FCL is pretty big as it is and adding more simple fucntionalities, ironically, would add more complexity other than flexibility. Besides from that, they need to evaluate how much benefit this features would bring to developers and their cost, which it might seem very low for this funtion, but it really isn't. You code it in 1 minute, but Microsoft simply can't do that. They need to document, test, translate and a whole lot more of things for it.

    This blog post by Eric Lippert illustrates this.

    After all, as you very well said on the openning post, it is very simple for you to write this yourself.

    Regards,
    Fernando.
    I always try to Keep it Sharp & simple.
    • Proposed as answer by Harry Zhu Friday, August 14, 2009 7:00 AM
    • Marked as answer by Harry Zhu Tuesday, August 18, 2009 8:46 AM
    Friday, August 7, 2009 7:05 PM
  • Are you talking about GetRange?

    Consider the differences between how normal it would be to use GetRange against a stepped GetRange. Probably 100 to 1.

    I always try to Keep it Sharp & simple.
    • Proposed as answer by Harry Zhu Friday, August 14, 2009 7:00 AM
    • Marked as answer by Harry Zhu Tuesday, August 18, 2009 8:46 AM
    Friday, August 7, 2009 7:35 PM

All replies

  • No big deal, the user can cast the results as a List<Int32> or an array of Int32 and perform any step they want anyhow.
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Friday, August 7, 2009 5:28 PM
  • //Example in VB.NET
    For i As Integer = 0 To 10 Step 2
    ............
    Next

    //C#
    for (int i = 0; i <= 10; i += 2)
    {
     ..........
    }


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Friday, August 7, 2009 5:34 PM
  • so if I have Enumerable.Range(0,21) and I want to step 2, how do I do ?
    Friday, August 7, 2009 5:34 PM
  • John, I have made the function, but I wonder if I can use the filter this list so that it's stepped:

    Enumerable.Range(0,21).ToList().....

    or maybe I can do something like

    from r in Enumerable.Range(0,21)
    where ...
    select ....

    I wonder how?
    Friday, August 7, 2009 5:38 PM
  • List<Int32> list = Enumerable.Range(0,21).ToList();
    for (int i = 0; i <= list.Count; i += 2)
    {
       .....
    }

    Note: I am not in front of VS at the moment, so if there any typo's I apologize
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Proposed as answer by Harry Zhu Friday, August 14, 2009 7:00 AM
    • Marked as answer by Harry Zhu Tuesday, August 18, 2009 8:46 AM
    Friday, August 7, 2009 5:49 PM
  •             var bytwos = Enumerable.Range(0, 21).Where(value => (value % 2) == 0);

    I have to say, there's no elegance in this kind of code.
    Hans Passant.
    • Proposed as answer by Harry Zhu Friday, August 14, 2009 7:00 AM
    • Marked as answer by Harry Zhu Tuesday, August 18, 2009 8:46 AM
    Friday, August 7, 2009 6:55 PM
    Moderator
  • I think that what is important here (for aguments sake) is that Microsoft can't implement every simple feature that they can think of. The FCL is pretty big as it is and adding more simple fucntionalities, ironically, would add more complexity other than flexibility. Besides from that, they need to evaluate how much benefit this features would bring to developers and their cost, which it might seem very low for this funtion, but it really isn't. You code it in 1 minute, but Microsoft simply can't do that. They need to document, test, translate and a whole lot more of things for it.

    This blog post by Eric Lippert illustrates this.

    After all, as you very well said on the openning post, it is very simple for you to write this yourself.

    Regards,
    Fernando.
    I always try to Keep it Sharp & simple.
    • Proposed as answer by Harry Zhu Friday, August 14, 2009 7:00 AM
    • Marked as answer by Harry Zhu Tuesday, August 18, 2009 8:46 AM
    Friday, August 7, 2009 7:05 PM
  • Then why does Microsoft make such a function there? any special purpose?
    Friday, August 7, 2009 7:30 PM
  • Are you talking about GetRange?

    Consider the differences between how normal it would be to use GetRange against a stepped GetRange. Probably 100 to 1.

    I always try to Keep it Sharp & simple.
    • Proposed as answer by Harry Zhu Friday, August 14, 2009 7:00 AM
    • Marked as answer by Harry Zhu Tuesday, August 18, 2009 8:46 AM
    Friday, August 7, 2009 7:35 PM