| 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)) => new MappedListIterable<E, T>(this, f); |
| 176 new MappedListIterable<E, dynamic/*=T*/ >(this, f); | |
| 177 | 176 |
| 178 E reduce(E combine(var value, E element)) { | 177 E reduce(E combine(var value, E element)) { |
| 179 int length = this.length; | 178 int length = this.length; |
| 180 if (length == 0) throw IterableElementError.noElement(); | 179 if (length == 0) throw IterableElementError.noElement(); |
| 181 E value = elementAt(0); | 180 E value = elementAt(0); |
| 182 for (int i = 1; i < length; i++) { | 181 for (int i = 1; i < length; i++) { |
| 183 value = combine(value, elementAt(i)); | 182 value = combine(value, elementAt(i)); |
| 184 if (length != this.length) { | 183 if (length != this.length) { |
| 185 throw new ConcurrentModificationError(this); | 184 throw new ConcurrentModificationError(this); |
| 186 } | 185 } |
| 187 | 186 |
| 188 } | 187 } |
| 189 return value; | 188 return value; |
| 190 } | 189 } |
| 191 | 190 |
| 192 /*=T*/ fold/*<T>*/( | 191 T fold<T>(T initialValue, T combine(T previousValue, E element)) { |
| 193 var/*=T*/ initialValue, /*=T*/ combine( | |
| 194 var/*=T*/ previousValue, E element)) { | |
| 195 var value = initialValue; | 192 var value = initialValue; |
| 196 int length = this.length; | 193 int length = this.length; |
| 197 for (int i = 0; i < length; i++) { | 194 for (int i = 0; i < length; i++) { |
| 198 value = combine(value, elementAt(i)); | 195 value = combine(value, elementAt(i)); |
| 199 if (length != this.length) { | 196 if (length != this.length) { |
| 200 throw new ConcurrentModificationError(this); | 197 throw new ConcurrentModificationError(this); |
| 201 } | 198 } |
| 202 } | 199 } |
| 203 return value; | 200 return value; |
| 204 } | 201 } |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 | 418 |
| 422 class WhereIterable<E> extends Iterable<E> { | 419 class WhereIterable<E> extends Iterable<E> { |
| 423 final Iterable<E> _iterable; | 420 final Iterable<E> _iterable; |
| 424 final _ElementPredicate<E> _f; | 421 final _ElementPredicate<E> _f; |
| 425 | 422 |
| 426 WhereIterable(this._iterable, bool this._f(E element)); | 423 WhereIterable(this._iterable, bool this._f(E element)); |
| 427 | 424 |
| 428 Iterator<E> get iterator => new WhereIterator<E>(_iterable.iterator, _f); | 425 Iterator<E> get iterator => new WhereIterator<E>(_iterable.iterator, _f); |
| 429 | 426 |
| 430 // Specialization of [Iterable.map] to non-EfficientLengthIterable. | 427 // Specialization of [Iterable.map] to non-EfficientLengthIterable. |
| 431 Iterable/*<T>*/ map/*<T>*/(/*=T*/ f(E element)) => | 428 Iterable<T> map<T>(T f(E element)) => new MappedIterable<E, T>._(this, f); |
| 432 new MappedIterable<E, dynamic/*=T*/>._(this, f); | |
| 433 } | 429 } |
| 434 | 430 |
| 435 class WhereIterator<E> extends Iterator<E> { | 431 class WhereIterator<E> extends Iterator<E> { |
| 436 final Iterator<E> _iterator; | 432 final Iterator<E> _iterator; |
| 437 final _ElementPredicate _f; | 433 final _ElementPredicate _f; |
| 438 | 434 |
| 439 WhereIterator(this._iterator, bool this._f(E element)); | 435 WhereIterator(this._iterator, bool this._f(E element)); |
| 440 | 436 |
| 441 bool moveNext() { | 437 bool moveNext() { |
| 442 while (_iterator.moveNext()) { | 438 while (_iterator.moveNext()) { |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 713 | 709 |
| 714 E singleWhere(bool test(E element), { E orElse() }) { | 710 E singleWhere(bool test(E element), { E orElse() }) { |
| 715 if (orElse != null) return orElse(); | 711 if (orElse != null) return orElse(); |
| 716 throw IterableElementError.noElement(); | 712 throw IterableElementError.noElement(); |
| 717 } | 713 } |
| 718 | 714 |
| 719 String join([String separator = ""]) => ""; | 715 String join([String separator = ""]) => ""; |
| 720 | 716 |
| 721 Iterable<E> where(bool test(E element)) => this; | 717 Iterable<E> where(bool test(E element)) => this; |
| 722 | 718 |
| 723 Iterable/*<T>*/ map/*<T>*/(/*=T*/ f(E element)) => const EmptyIterable(); | 719 Iterable<T> map<T>(T f(E element)) => const EmptyIterable(); |
| 724 | 720 |
| 725 E reduce(E combine(E value, E element)) { | 721 E reduce(E combine(E value, E element)) { |
| 726 throw IterableElementError.noElement(); | 722 throw IterableElementError.noElement(); |
| 727 } | 723 } |
| 728 | 724 |
| 729 /*=T*/ fold/*<T>*/( | 725 T fold<T>(T initialValue, T combine(T previousValue, E element)) { |
| 730 var/*=T*/ initialValue, /*=T*/ combine( | |
| 731 var/*=T*/ previousValue, E element)) { | |
| 732 return initialValue; | 726 return initialValue; |
| 733 } | 727 } |
| 734 | 728 |
| 735 Iterable<E> skip(int count) { | 729 Iterable<E> skip(int count) { |
| 736 RangeError.checkNotNegative(count, "count"); | 730 RangeError.checkNotNegative(count, "count"); |
| 737 return this; | 731 return this; |
| 738 } | 732 } |
| 739 | 733 |
| 740 Iterable<E> skipWhile(bool test(E element)) => this; | 734 Iterable<E> skipWhile(bool test(E element)) => this; |
| 741 | 735 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 762 * Creates errors throw by [Iterable] when the element count is wrong. | 756 * Creates errors throw by [Iterable] when the element count is wrong. |
| 763 */ | 757 */ |
| 764 abstract class IterableElementError { | 758 abstract class IterableElementError { |
| 765 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ | 759 /** Error thrown thrown by, e.g., [Iterable.first] when there is no result. */ |
| 766 static StateError noElement() => new StateError("No element"); | 760 static StateError noElement() => new StateError("No element"); |
| 767 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ | 761 /** Error thrown by, e.g., [Iterable.single] if there are too many results. */ |
| 768 static StateError tooMany() => new StateError("Too many elements"); | 762 static StateError tooMany() => new StateError("Too many elements"); |
| 769 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ | 763 /** Error thrown by, e.g., [List.setRange] if there are too few elements. */ |
| 770 static StateError tooFew() => new StateError("Too few elements"); | 764 static StateError tooFew() => new StateError("Too few elements"); |
| 771 } | 765 } |
| OLD | NEW |