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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« sdk/lib/core/iterable.dart ('K') | « sdk/lib/core/iterable.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.core; 5 part of dart.core;
6 6
7 /** 7 /**
8 * An indexable collection of objects with a length. 8 * An indexable collection of objects with a length.
9 * 9 *
10 * Subclasses of this class implement different kinds of lists. 10 * Subclasses of this class implement different kinds of lists.
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 } else { 111 } else {
112 result = new List<E>(length); 112 result = new List<E>(length);
113 } 113 }
114 for (int i = 0; i < length; i++) { 114 for (int i = 0; i < length; i++) {
115 result[i] = generator(i); 115 result[i] = generator(i);
116 } 116 }
117 return result; 117 return result;
118 } 118 }
119 119
120 /** 120 /**
121 * Create a list of values between `from` and `to`.
122 *
123 * The list starts with `from` as the first value, and then
124 * progresses towards `to` in increments of `step`, and stops when reaching
125 * or overshooting `to`. If it hits `to` the `to` value is included,
126 * otherwise it isn't.
127 *
128 * If `to` is less than `from`, the values are in decreasing order.
129 *
130 * The [step] value must always be positive. It defaults to `1`.
131 *
132 * If [growable] is true, the resulting list is growable, otherwise
133 * it defaults to a fixed-length list.
134 */
135 static List<int> range(int from, int to, {int step: 1, growable: false}) {
136 if (step <= 0) {
137 throw new RangeError.value("Step must be positive, was $step");
138 }
139 final int delta = to - from;
140 if (delta < 0) {
141 step = -step;
142 }
143 final int steps = delta ~/ step + 1;
144 List list = growable ? new List<int>()..length = steps
145 : new List<int>(steps);
146 for (int i = 0; i < steps; i++) {
147 list[i] = from + i * step;
148 }
149 return list;
150 }
151
152 /**
121 * Returns the object at the given [index] in the list 153 * Returns the object at the given [index] in the list
122 * or throws a [RangeError] if [index] is out of bounds. 154 * or throws a [RangeError] if [index] is out of bounds.
123 */ 155 */
124 E operator [](int index); 156 E operator [](int index);
125 157
126 /** 158 /**
127 * Sets the value at the given [index] in the list to [value] 159 * Sets the value at the given [index] in the list to [value]
128 * or throws a [RangeError] if [index] is out of bounds. 160 * or throws a [RangeError] if [index] is out of bounds.
129 */ 161 */
130 void operator []=(int index, E value); 162 void operator []=(int index, E value);
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 * as values. The `Map.keys` [Iterable] iterates the indices of this list 479 * as values. The `Map.keys` [Iterable] iterates the indices of this list
448 * in numerical order. 480 * in numerical order.
449 * 481 *
450 * List<String> words = ['fee', 'fi', 'fo', 'fum']; 482 * List<String> words = ['fee', 'fi', 'fo', 'fum'];
451 * Map<int, String> map = words.asMap(); 483 * Map<int, String> map = words.asMap();
452 * map[0] + map[1]; // 'feefi'; 484 * map[0] + map[1]; // 'feefi';
453 * map.keys.toList(); // [0, 1, 2, 3] 485 * map.keys.toList(); // [0, 1, 2, 3]
454 */ 486 */
455 Map<int, E> asMap(); 487 Map<int, E> asMap();
456 } 488 }
OLDNEW
« 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