OLD | NEW |
---|---|
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._internal; | 5 part of dart._internal; |
6 | 6 |
7 /** | 7 /** |
8 * Marker interface for [Iterable] subclasses that have an efficient | 8 * Marker interface for [Iterable] subclasses that have an efficient |
9 * [length] implementation. | 9 * [length] implementation. |
10 */ | 10 */ |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 if (length != this.length) { | 165 if (length != this.length) { |
166 throw new ConcurrentModificationError(this); | 166 throw new ConcurrentModificationError(this); |
167 } | 167 } |
168 } | 168 } |
169 return buffer.toString(); | 169 return buffer.toString(); |
170 } | 170 } |
171 } | 171 } |
172 | 172 |
173 Iterable<E> where(bool test(E element)) => super.where(test); | 173 Iterable<E> where(bool test(E element)) => super.where(test); |
174 | 174 |
175 Iterable/*<T>*/ map/*<T>*/(/*=T*/ f(E element)) => | 175 Iterable<T> map<T>(T f(E element)) => |
176 new MappedListIterable<E, dynamic/*=T*/ >(this, f); | 176 new MappedListIterable<E, T >(this, f); |
177 | 177 |
178 E reduce(E combine(var value, E element)) { | 178 E reduce(E combine(var value, E element)) { |
179 int length = this.length; | 179 int length = this.length; |
180 if (length == 0) throw IterableElementError.noElement(); | 180 if (length == 0) throw IterableElementError.noElement(); |
181 E value = elementAt(0); | 181 E value = elementAt(0); |
182 for (int i = 1; i < length; i++) { | 182 for (int i = 1; i < length; i++) { |
183 value = combine(value, elementAt(i)); | 183 value = combine(value, elementAt(i)); |
184 if (length != this.length) { | 184 if (length != this.length) { |
185 throw new ConcurrentModificationError(this); | 185 throw new ConcurrentModificationError(this); |
186 } | 186 } |
187 | 187 |
188 } | 188 } |
189 return value; | 189 return value; |
190 } | 190 } |
191 | 191 |
192 /*=T*/ fold/*<T>*/( | 192 T fold<T>( |
floitsch
2016/12/19 19:14:45
should fit.
Lasse Reichstein Nielsen
2017/01/03 10:33:20
Done.
| |
193 var/*=T*/ initialValue, /*=T*/ combine( | 193 T initialValue, T combine( |
194 var/*=T*/ previousValue, E element)) { | 194 T previousValue, E element)) { |
195 var value = initialValue; | 195 var value = initialValue; |
196 int length = this.length; | 196 int length = this.length; |
197 for (int i = 0; i < length; i++) { | 197 for (int i = 0; i < length; i++) { |
198 value = combine(value, elementAt(i)); | 198 value = combine(value, elementAt(i)); |
199 if (length != this.length) { | 199 if (length != this.length) { |
200 throw new ConcurrentModificationError(this); | 200 throw new ConcurrentModificationError(this); |
201 } | 201 } |
202 } | 202 } |
203 return value; | 203 return value; |
204 } | 204 } |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
421 | 421 |
422 class WhereIterable<E> extends Iterable<E> { | 422 class WhereIterable<E> extends Iterable<E> { |
423 final Iterable<E> _iterable; | 423 final Iterable<E> _iterable; |
424 final _ElementPredicate<E> _f; | 424 final _ElementPredicate<E> _f; |
425 | 425 |
426 WhereIterable(this._iterable, bool this._f(E element)); | 426 WhereIterable(this._iterable, bool this._f(E element)); |
427 | 427 |
428 Iterator<E> get iterator => new WhereIterator<E>(_iterable.iterator, _f); | 428 Iterator<E> get iterator => new WhereIterator<E>(_iterable.iterator, _f); |
429 | 429 |
430 // Specialization of [Iterable.map] to non-EfficientLengthIterable. | 430 // Specialization of [Iterable.map] to non-EfficientLengthIterable. |
431 Iterable/*<T>*/ map/*<T>*/(/*=T*/ f(E element)) => | 431 Iterable<T> map<T>(T f(E element)) => |
432 new MappedIterable<E, dynamic/*=T*/>._(this, f); | 432 new MappedIterable<E, T>._(this, f); |
433 } | 433 } |
434 | 434 |
435 class WhereIterator<E> extends Iterator<E> { | 435 class WhereIterator<E> extends Iterator<E> { |
436 final Iterator<E> _iterator; | 436 final Iterator<E> _iterator; |
437 final _ElementPredicate _f; | 437 final _ElementPredicate _f; |
438 | 438 |
439 WhereIterator(this._iterator, bool this._f(E element)); | 439 WhereIterator(this._iterator, bool this._f(E element)); |
440 | 440 |
441 bool moveNext() { | 441 bool moveNext() { |
442 while (_iterator.moveNext()) { | 442 while (_iterator.moveNext()) { |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
713 | 713 |
714 E singleWhere(bool test(E element), { E orElse() }) { | 714 E singleWhere(bool test(E element), { E orElse() }) { |
715 if (orElse != null) return orElse(); | 715 if (orElse != null) return orElse(); |
716 throw IterableElementError.noElement(); | 716 throw IterableElementError.noElement(); |
717 } | 717 } |
718 | 718 |
719 String join([String separator = ""]) => ""; | 719 String join([String separator = ""]) => ""; |
720 | 720 |
721 Iterable<E> where(bool test(E element)) => this; | 721 Iterable<E> where(bool test(E element)) => this; |
722 | 722 |
723 Iterable/*<T>*/ map/*<T>*/(/*=T*/ f(E element)) => const EmptyIterable(); | 723 Iterable<T> map<T>(T f(E element)) => const EmptyIterable(); |
724 | 724 |
725 E reduce(E combine(E value, E element)) { | 725 E reduce(E combine(E value, E element)) { |
726 throw IterableElementError.noElement(); | 726 throw IterableElementError.noElement(); |
727 } | 727 } |
728 | 728 |
729 /*=T*/ fold/*<T>*/( | 729 T fold<T>( |
floitsch
2016/12/19 19:14:45
should fit.
Lasse Reichstein Nielsen
2017/01/03 10:33:20
Done.
| |
730 var/*=T*/ initialValue, /*=T*/ combine( | 730 T initialValue, T combine( |
731 var/*=T*/ previousValue, E element)) { | 731 T previousValue, E element)) { |
732 return initialValue; | 732 return initialValue; |
733 } | 733 } |
734 | 734 |
735 Iterable<E> skip(int count) { | 735 Iterable<E> skip(int count) { |
736 RangeError.checkNotNegative(count, "count"); | 736 RangeError.checkNotNegative(count, "count"); |
737 return this; | 737 return this; |
738 } | 738 } |
739 | 739 |
740 Iterable<E> skipWhile(bool test(E element)) => this; | 740 Iterable<E> skipWhile(bool test(E element)) => this; |
741 | 741 |
(...skipping 20 matching lines...) Expand all Loading... | |
762 * Creates errors throw by [Iterable] when the element count is wrong. | 762 * Creates errors throw by [Iterable] when the element count is wrong. |
763 */ | 763 */ |
764 abstract class IterableElementError { | 764 abstract class IterableElementError { |
765 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ | 765 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ |
766 static StateError noElement() => new StateError("No element"); | 766 static StateError noElement() => new StateError("No element"); |
767 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ | 767 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ |
768 static StateError tooMany() => new StateError("Too many elements"); | 768 static StateError tooMany() => new StateError("Too many elements"); |
769 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ | 769 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ |
770 static StateError tooFew() => new StateError("Too few elements"); | 770 static StateError tooFew() => new StateError("Too few elements"); |
771 } | 771 } |
OLD | NEW |