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

Side by Side Diff: sdk/lib/collection/list.dart

Issue 14071002: Added new version of reduce. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Removed more uses of max, and a few bugs. Created 7 years, 8 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
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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.collection; 5 part of dart.collection;
6 6
7 /** 7 /**
8 * Abstract implementation of a list. 8 * Abstract implementation of a list.
9 * 9 *
10 * All operations are defined in terms of `length`, `operator[]`, 10 * All operations are defined in terms of `length`, `operator[]`,
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 match = element; 134 match = element;
135 } 135 }
136 if (length != this.length) { 136 if (length != this.length) {
137 throw new ConcurrentModificationError(this); 137 throw new ConcurrentModificationError(this);
138 } 138 }
139 } 139 }
140 if (matchFound) return match; 140 if (matchFound) return match;
141 throw new StateError("No matching element"); 141 throw new StateError("No matching element");
142 } 142 }
143 143
144 E min([int compare(E a, E b)]) {
145 if (length == 0) return null;
146 if (compare == null) {
147 var defaultCompare = Comparable.compare;
148 compare = defaultCompare;
149 }
150 E min = this[0];
151 int length = this.length;
152 for (int i = 1; i < length; i++) {
153 E element = this[i];
154 if (compare(min, element) > 0) {
155 min = element;
156 }
157 if (length != this.length) {
158 throw new ConcurrentModificationError(this);
159 }
160 }
161 return min;
162 }
163
164 E max([int compare(E a, E b)]) {
165 if (length == 0) return null;
166 if (compare == null) {
167 var defaultCompare = Comparable.compare;
168 compare = defaultCompare;
169 }
170 E max = this[0];
171 int length = this.length;
172 for (int i = 1; i < length; i++) {
173 E element = this[i];
174 if (compare(max, element) < 0) {
175 max = element;
176 }
177 if (length != this.length) {
178 throw new ConcurrentModificationError(this);
179 }
180 }
181 return max;
182 }
183
184 String join([String separator = ""]) { 144 String join([String separator = ""]) {
185 int length = this.length; 145 int length = this.length;
186 if (!separator.isEmpty) { 146 if (!separator.isEmpty) {
187 if (length == 0) return ""; 147 if (length == 0) return "";
188 String first = "${this[0]}"; 148 String first = "${this[0]}";
189 if (length != this.length) { 149 if (length != this.length) {
190 throw new ConcurrentModificationError(this); 150 throw new ConcurrentModificationError(this);
191 } 151 }
192 StringBuffer buffer = new StringBuffer(first); 152 StringBuffer buffer = new StringBuffer(first);
193 for (int i = 1; i < length; i++) { 153 for (int i = 1; i < length; i++) {
(...skipping 13 matching lines...) Expand all
207 } 167 }
208 } 168 }
209 return buffer.toString(); 169 return buffer.toString();
210 } 170 }
211 } 171 }
212 172
213 Iterable<E> where(bool test(E element)) => new WhereIterable<E>(this, test); 173 Iterable<E> where(bool test(E element)) => new WhereIterable<E>(this, test);
214 174
215 Iterable map(f(E element)) => new MappedListIterable(this, f); 175 Iterable map(f(E element)) => new MappedListIterable(this, f);
216 176
217 reduce(var initialValue, combine(var previousValue, E element)) { 177 E reduce(E combine(E previousValue, E element)) {
218 return fold(initialValue, combine); 178 if (length == 0) throw new StateError("No elements");
179 E value = this[0];
180 for (int i = 1; i < length; i++) {
181 value = combine(value, this[i]);
182 }
183 return value;
219 } 184 }
220 185
221 fold(var initialValue, combine(var previousValue, E element)) { 186 fold(var initialValue, combine(var previousValue, E element)) {
222 var value = initialValue; 187 var value = initialValue;
223 int length = this.length; 188 int length = this.length;
224 for (int i = 0; i < length; i++) { 189 for (int i = 0; i < length; i++) {
225 value = combine(value, this[i]); 190 value = combine(value, this[i]);
226 if (length != this.length) { 191 if (length != this.length) {
227 throw new ConcurrentModificationError(this); 192 throw new ConcurrentModificationError(this);
228 } 193 }
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 for (int i = startIndex; i >= 0; i--) { 423 for (int i = startIndex; i >= 0; i--) {
459 if (this[i] == element) { 424 if (this[i] == element) {
460 return i; 425 return i;
461 } 426 }
462 } 427 }
463 return -1; 428 return -1;
464 } 429 }
465 430
466 Iterable<E> get reversed => new ReversedListIterable(this); 431 Iterable<E> get reversed => new ReversedListIterable(this);
467 } 432 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698