Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(197)

Unified Diff: sdk/lib/core/list.dart

Issue 551063003: Add Iterable.range, List.range and int.to. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« sdk/lib/core/iterable.dart ('K') | « sdk/lib/core/iterable.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/core/list.dart
diff --git a/sdk/lib/core/list.dart b/sdk/lib/core/list.dart
index 997b2c3f31ad09afcfa93a96a6975b1857ae5cfc..82e60a16930abc3dcc9cddcf5e18082f03a055a4 100644
--- a/sdk/lib/core/list.dart
+++ b/sdk/lib/core/list.dart
@@ -118,6 +118,38 @@ abstract class List<E> implements Iterable<E>, EfficientLength {
}
/**
+ * Create a list of values between `from` and `to`.
+ *
+ * The list starts with `from` as the first value, and then
+ * progresses towards `to` in increments of `step`, and stops when reaching
+ * or overshooting `to`. If it hits `to` the `to` value is included,
+ * otherwise it isn't.
+ *
+ * If `to` is less than `from`, the values are in decreasing order.
+ *
+ * The [step] value must always be positive. It defaults to `1`.
+ *
+ * If [growable] is true, the resulting list is growable, otherwise
+ * it defaults to a fixed-length list.
+ */
+ static List<int> range(int from, int to, {int step: 1, growable: false}) {
+ if (step <= 0) {
+ throw new RangeError.value("Step must be positive, was $step");
+ }
+ final int delta = to - from;
+ if (delta < 0) {
+ step = -step;
+ }
+ final int steps = delta ~/ step + 1;
+ List list = growable ? new List<int>()..length = steps
+ : new List<int>(steps);
+ for (int i = 0; i < steps; i++) {
+ list[i] = from + i * step;
+ }
+ return list;
+ }
+
+ /**
* Returns the object at the given [index] in the list
* or throws a [RangeError] if [index] is out of bounds.
*/
« sdk/lib/core/iterable.dart ('K') | « sdk/lib/core/iterable.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698