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

Side by Side Diff: sdk/lib/core/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.core; 5 part of dart.core;
6 6
7 /** 7 /**
8 * The [Iterable] interface allows to get an [Iterator] out of an 8 * The [Iterable] interface allows to get an [Iterator] out of an
9 * [Iterable] object. 9 * [Iterable] object.
10 * 10 *
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 void forEach(void f(E element)) { 91 void forEach(void f(E element)) {
92 for (E element in this) f(element); 92 for (E element in this) f(element);
93 } 93 }
94 94
95 /** 95 /**
96 * Reduce a collection to a single value by iteratively combining each element 96 * Reduce a collection to a single value by iteratively combining each element
97 * of the collection with an existing value using the provided function. 97 * of the collection with an existing value using the provided function.
98 * Use [initialValue] as the initial value, and the function [combine] to 98 * Use [initialValue] as the initial value, and the function [combine] to
99 * create a new value from the previous one and an element. 99 * create a new value from the previous one and an element.
100 * 100 *
101 * Example of calculating the sum of a collection: 101 * Example of calculating the sum of an iterable:
102 * 102 *
103 * collection.reduce(0, (prev, element) => prev + element); 103 * iterable.reduce((prev, element) => prev + element);
104 *
105 * *UPCOMING API-CHANGE*: this method will soon be changed to not take
106 * an initial value: `iterable.reduce(min)`. Use [fold] instead.
kevmoo-old 2013/04/03 17:45:51 I'd actually mark this API as @deprecated. It can
Lasse Reichstein Nielsen 2013/04/04 08:35:18 Agree. Mark as deprecated. Anyone using the curren
floitsch 2013/04/05 16:10:03 Done.
104 */ 107 */
105 dynamic reduce(var initialValue, 108 dynamic reduce(var initialValue,
106 dynamic combine(var previousValue, E element)) { 109 dynamic combine(var previousValue, E element)) {
110 return fold(initialValue, combine);
111 }
112
113 /**
114 * Reduce a collection to a single value by iteratively combining each element
115 * of the collection with an existing value using the provided function.
116 * Use [initialValue] as the initial value, and the function [combine] to
117 * create a new value from the previous one and an element.
118 *
119 * Example of calculating the sum of an iterable:
120 *
121 * iterable.fold(0, (prev, element) => prev + element);
122 */
123 dynamic fold(var initialValue,
124 dynamic combine(var previousValue, E element)) {
107 var value = initialValue; 125 var value = initialValue;
108 for (E element in this) value = combine(value, element); 126 for (E element in this) value = combine(value, element);
109 return value; 127 return value;
110 } 128 }
111 129
112 /** 130 /**
113 * Returns true if every elements of this collection satisify the 131 * Returns true if every elements of this collection satisify the
114 * predicate [f]. Returns false otherwise. 132 * predicate [f]. Returns false otherwise.
115 */ 133 */
116 bool every(bool f(E element)) { 134 bool every(bool f(E element)) {
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 */ 457 */
440 abstract class BidirectionalIterator<T> extends Iterator<T> { 458 abstract class BidirectionalIterator<T> extends Iterator<T> {
441 /** 459 /**
442 * Move back to the previous element. 460 * Move back to the previous element.
443 * 461 *
444 * Returns true and updates [current] if successful. Returns false 462 * Returns true and updates [current] if successful. Returns false
445 * and sets [current] to null if there is no previous element. 463 * and sets [current] to null if there is no previous element.
446 */ 464 */
447 bool movePrevious(); 465 bool movePrevious();
448 } 466 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698