Sdk: Python-style range() function and iterable

Created on 8 Jan 2013  路  10Comments  路  Source: dart-lang/sdk

range() would be useful for for..in/.forEach() loops, constructing lists of integers, and in combinations with functions like zip().

The signature in Dart might be:

    Iterable<int> range(int stop_or_start, [int stop, int step]);

or possibly named arguments for stop and step.

Usage:

    new List.from(range(10)); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    new List.from(range(1, 11)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    new List.from(range(1, 10, 2)); // [1, 3, 5, 7, 9]
    new List.from(range(10, 0, -1)); // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

See Python docs:
http://docs.python.org/3.2/library/functions.html#range

P2 area-library core-n type-enhancement

Most helpful comment

You can also use new Iterable.generate(n) to make the range 0..n-1.
It's not short and crispy, though.

All 10 comments

Maybe more interesting would be:
1.to(10) returning an Iterable going from 1 to 10 inclusive.
potentially with a step:
1.to(10, step: 2)

cc @lrhn.

_This comment was originally written by Ilya.Khar...@gmail.com_


There is an example implementation of this:

import "range.dart";
for (int i in range(0, 5)) {
    print (i); //0,1,2,3,4,5
}

https://github.com/ilyakharlamov/dart-range

_This comment was originally written by reeves...@gmail.com_


Please add an "Iterable to(int max, {int step})" method to the int class.
This would be extremely helpful for Dart unit testing.

After a very recent change, you can do:
  new Iterable.generate(5)
to get an iterable of (0, 1, 2, 3, 4).
I just made the generator function optional, defaulting to (int x)=>x.

I'm a little wary at adding functions to the core libraries with the argument that it will be helpful for unit testing. Those functions can be put into the unit-test library instead. If it's generally useful, that's another case.

We frequently discussed such an operator, but usually got stuck at the semantics: should 0.to(2) include or exclude "2"?
There are use-cases for both, and depending on the context it is extremely easy to misread the code. Even in this bug-report we already encountered both:
original post (at the top): range(1, 11) -> [1, ..., 10]
comment #颅1 and #颅3: range(0, 5) => 0, ..., 5
Then there is the list.getRange method, which is again exclusive.

It might be over-protective, but this was also a reason not to add it.

_This comment was originally written by reeves.ru...@gmail.com_


It's an established convention that Iterables typically don't include the upper bound. The int.to() proposal would also work for ranges that don't start at zero. For example consider "isPrime()":

isPrime(n) => n>1 && !2.to(n).any((i) => n % i == 0);

"2.to(n)" is better than "new Iterable.generate(n - 2, (i) => i + 2)"

_This comment was originally written by reeves....@gmail.com_


I sense this is diverging from the original range() proposal so I've opened a new issue specifically for int.to():
https://code.google.com/p/dart/issues/detail?id=20835

I should point out for anyone coming to this bug looking for range() that we've had it in quiver.iterables for a while: http://www.dartdocs.org/documentation/quiver/0.18.2/index.html#quiver/quiver-iterables#id_range

It's pretty much identical to Python 2.x's xrange() or 3.x's range()

You can also use new Iterable.generate(n) to make the range 0..n-1.
It's not short and crispy, though.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DartBot picture DartBot  路  3Comments

Hixie picture Hixie  路  3Comments

ranquild picture ranquild  路  3Comments

sgrekhov picture sgrekhov  路  3Comments

emilniklas picture emilniklas  路  3Comments