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

Side by Side Diff: sdk/lib/_collection_dev/iterable.dart

Issue 13548002: Add Iterable.fold (and Stream.fold) which replace `reduce`. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 } 193 }
194 return buffer.toString(); 194 return buffer.toString();
195 } 195 }
196 } 196 }
197 197
198 Iterable<E> where(bool test(E element)) => super.where(test); 198 Iterable<E> where(bool test(E element)) => super.where(test);
199 199
200 Iterable map(f(E element)) => new MappedListIterable(this, f); 200 Iterable map(f(E element)) => new MappedListIterable(this, f);
201 201
202 reduce(var initialValue, combine(var previousValue, E element)) { 202 reduce(var initialValue, combine(var previousValue, E element)) {
203 return fold(initialValue, combine);
204 }
205
206 fold(var initialValue, combine(var previousValue, E element)) {
203 var value = initialValue; 207 var value = initialValue;
204 int length = this.length; 208 int length = this.length;
205 for (int i = 0; i < length; i++) { 209 for (int i = 0; i < length; i++) {
206 value = combine(value, elementAt(i)); 210 value = combine(value, elementAt(i));
207 if (length != this.length) { 211 if (length != this.length) {
208 throw new ConcurrentModificationError(this); 212 throw new ConcurrentModificationError(this);
209 } 213 }
210 } 214 }
211 return value; 215 return value;
212 } 216 }
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after
663 667
664 E max([int compare(E a, E b)]) => null; 668 E max([int compare(E a, E b)]) => null;
665 669
666 String join([String separator]) => ""; 670 String join([String separator]) => "";
667 671
668 Iterable<E> where(bool test(E element)) => this; 672 Iterable<E> where(bool test(E element)) => this;
669 673
670 Iterable map(f(E element)) => const EmptyIterable(); 674 Iterable map(f(E element)) => const EmptyIterable();
671 675
672 reduce(var initialValue, combine(var previousValue, E element)) { 676 reduce(var initialValue, combine(var previousValue, E element)) {
677 return fold(initialValue, combine);
678 }
679
680 fold(var initialValue, combine(var previousValue, E element)) {
673 return initialValue; 681 return initialValue;
674 } 682 }
675 683
676 Iterable<E> skip(int count) => this; 684 Iterable<E> skip(int count) => this;
677 685
678 Iterable<E> skipWhile(bool test(E element)) => this; 686 Iterable<E> skipWhile(bool test(E element)) => this;
679 687
680 Iterable<E> take(int count) => this; 688 Iterable<E> take(int count) => this;
681 689
682 Iterable<E> takeWhile(bool test(E element)) => this; 690 Iterable<E> takeWhile(bool test(E element)) => this;
683 691
684 List toList({ bool growable: true }) => growable ? <E>[] : new List<E>(0); 692 List toList({ bool growable: true }) => growable ? <E>[] : new List<E>(0);
685 693
686 Set toSet() => new Set<E>(); 694 Set toSet() => new Set<E>();
687 } 695 }
688 696
689 /** The always empty iterator. */ 697 /** The always empty iterator. */
690 class EmptyIterator<E> implements Iterator<E> { 698 class EmptyIterator<E> implements Iterator<E> {
691 const EmptyIterator(); 699 const EmptyIterator();
692 bool moveNext() => false; 700 bool moveNext() => false;
693 E get current => null; 701 E get current => null;
694 } 702 }
695 703
696 /** An [Iterator] that can move in both directions. */ 704 /** An [Iterator] that can move in both directions. */
697 abstract class BidirectionalIterator<T> implements Iterator<T> { 705 abstract class BidirectionalIterator<T> implements Iterator<T> {
698 bool movePrevious(); 706 bool movePrevious();
699 } 707 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698