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

Unified Diff: sdk/lib/core/iterable.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
« no previous file with comments | « sdk/lib/core/int.dart ('k') | sdk/lib/core/list.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/core/iterable.dart
diff --git a/sdk/lib/core/iterable.dart b/sdk/lib/core/iterable.dart
index 6d10da9fa125fbe1b17cc9329c0bcf4544baa6c9..31e11b2e8f95468f5fb899c1825eaa0d95826211 100644
--- a/sdk/lib/core/iterable.dart
+++ b/sdk/lib/core/iterable.dart
@@ -54,6 +54,30 @@ abstract class Iterable<E> {
}
/**
+ * Create an `Iterable<int>` iterating from `from` to `to`, inclusive.
+ *
+ * The iteration 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,
floitsch 2014/09/09 09:37:14 Strongly oppose. We have a getRange in this class
+ * 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`.
+ */
+ static Iterable<int> range(int from, int to, {int step: 1}) {
+ 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;
+ return new Iterable<int>.generate(steps, (n) => from + n * step);
+ }
+
+ /**
* Returns a new `Iterator` that allows iterating the elements of this
* `Iterable`.
*/
« no previous file with comments | « sdk/lib/core/int.dart ('k') | sdk/lib/core/list.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698