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

Side by Side Diff: sdk/lib/_collection_dev/iterable.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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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.dev; 5 part of dart._collection.dev;
6 6
7 /** 7 /**
8 * An [Iterable] for classes that have efficient [length] and [elementAt]. 8 * An [Iterable] for classes that have efficient [length] and [elementAt].
9 * 9 *
10 * All other methods are implemented in terms of [length] and [elementAt], 10 * All other methods are implemented in terms of [length] and [elementAt],
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 match = element; 119 match = element;
120 } 120 }
121 if (length != this.length) { 121 if (length != this.length) {
122 throw new ConcurrentModificationError(this); 122 throw new ConcurrentModificationError(this);
123 } 123 }
124 } 124 }
125 if (matchFound) return match; 125 if (matchFound) return match;
126 throw new StateError("No matching element"); 126 throw new StateError("No matching element");
127 } 127 }
128 128
129 E min([int compare(E a, E b)]) {
130 if (length == 0) return null;
131 if (compare == null) {
132 var defaultCompare = Comparable.compare;
133 compare = defaultCompare;
134 }
135 E min = elementAt(0);
136 int length = this.length;
137 for (int i = 1; i < length; i++) {
138 E element = elementAt(i);
139 if (compare(min, element) > 0) {
140 min = element;
141 }
142 if (length != this.length) {
143 throw new ConcurrentModificationError(this);
144 }
145 }
146 return min;
147 }
148
149 E max([int compare(E a, E b)]) {
150 if (length == 0) return null;
151 if (compare == null) {
152 var defaultCompare = Comparable.compare;
153 compare = defaultCompare;
154 }
155 E max = elementAt(0);
156 int length = this.length;
157 for (int i = 1; i < length; i++) {
158 E element = elementAt(i);
159 if (compare(max, element) < 0) {
160 max = element;
161 }
162 if (length != this.length) {
163 throw new ConcurrentModificationError(this);
164 }
165 }
166 return max;
167 }
168
169 String join([String separator = ""]) { 129 String join([String separator = ""]) {
170 int length = this.length; 130 int length = this.length;
171 if (!separator.isEmpty) { 131 if (!separator.isEmpty) {
172 if (length == 0) return ""; 132 if (length == 0) return "";
173 String first = "${elementAt(0)}"; 133 String first = "${elementAt(0)}";
174 if (length != this.length) { 134 if (length != this.length) {
175 throw new ConcurrentModificationError(this); 135 throw new ConcurrentModificationError(this);
176 } 136 }
177 StringBuffer buffer = new StringBuffer(first); 137 StringBuffer buffer = new StringBuffer(first);
178 for (int i = 1; i < length; i++) { 138 for (int i = 1; i < length; i++) {
(...skipping 13 matching lines...) Expand all
192 } 152 }
193 } 153 }
194 return buffer.toString(); 154 return buffer.toString();
195 } 155 }
196 } 156 }
197 157
198 Iterable<E> where(bool test(E element)) => super.where(test); 158 Iterable<E> where(bool test(E element)) => super.where(test);
199 159
200 Iterable map(f(E element)) => new MappedListIterable(this, f); 160 Iterable map(f(E element)) => new MappedListIterable(this, f);
201 161
202 reduce(var initialValue, combine(var previousValue, E element)) { 162 E reduce(E combine(var value, E element)) {
203 return fold(initialValue, combine); 163 if (length == 0) throw new StateError("No elements");
164 E value = elementAt(0);
165 for (int i = 1; i < length; i++) {
166 value = combine(value, elementAt(i));
167 }
168 return value;
204 } 169 }
205 170
206 fold(var initialValue, combine(var previousValue, E element)) { 171 fold(var initialValue, combine(var previousValue, E element)) {
207 var value = initialValue; 172 var value = initialValue;
208 int length = this.length; 173 int length = this.length;
209 for (int i = 0; i < length; i++) { 174 for (int i = 0; i < length; i++) {
210 value = combine(value, elementAt(i)); 175 value = combine(value, elementAt(i));
211 if (length != this.length) { 176 if (length != this.length) {
212 throw new ConcurrentModificationError(this); 177 throw new ConcurrentModificationError(this);
213 } 178 }
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 E lastWhere(bool test(E element), { E orElse() }) { 621 E lastWhere(bool test(E element), { E orElse() }) {
657 if (orElse != null) return orElse(); 622 if (orElse != null) return orElse();
658 throw new StateError("No matching element"); 623 throw new StateError("No matching element");
659 } 624 }
660 625
661 E singleWhere(bool test(E element), { E orElse() }) { 626 E singleWhere(bool test(E element), { E orElse() }) {
662 if (orElse != null) return orElse(); 627 if (orElse != null) return orElse();
663 throw new StateError("No matching element"); 628 throw new StateError("No matching element");
664 } 629 }
665 630
666 E min([int compare(E a, E b)]) => null;
667
668 E max([int compare(E a, E b)]) => null;
669
670 String join([String separator = ""]) => ""; 631 String join([String separator = ""]) => "";
671 632
672 Iterable<E> where(bool test(E element)) => this; 633 Iterable<E> where(bool test(E element)) => this;
673 634
674 Iterable map(f(E element)) => const EmptyIterable(); 635 Iterable map(f(E element)) => const EmptyIterable();
675 636
676 reduce(var initialValue, combine(var previousValue, E element)) { 637 E reduce(E combine(E value, E element)) {
677 return fold(initialValue, combine); 638 throw new StateError("No elements");
678 } 639 }
679 640
680 fold(var initialValue, combine(var previousValue, E element)) { 641 fold(var initialValue, combine(var previousValue, E element)) {
681 return initialValue; 642 return initialValue;
682 } 643 }
683 644
684 Iterable<E> skip(int count) => this; 645 Iterable<E> skip(int count) => this;
685 646
686 Iterable<E> skipWhile(bool test(E element)) => this; 647 Iterable<E> skipWhile(bool test(E element)) => this;
687 648
(...skipping 10 matching lines...) Expand all
698 class EmptyIterator<E> implements Iterator<E> { 659 class EmptyIterator<E> implements Iterator<E> {
699 const EmptyIterator(); 660 const EmptyIterator();
700 bool moveNext() => false; 661 bool moveNext() => false;
701 E get current => null; 662 E get current => null;
702 } 663 }
703 664
704 /** An [Iterator] that can move in both directions. */ 665 /** An [Iterator] that can move in both directions. */
705 abstract class BidirectionalIterator<T> implements Iterator<T> { 666 abstract class BidirectionalIterator<T> implements Iterator<T> {
706 bool movePrevious(); 667 bool movePrevious();
707 } 668 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698