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

Unified 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: Address comments. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/collection/collections.dart ('k') | sdk/lib/html/dart2js/html_dart2js.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/core/iterable.dart
diff --git a/sdk/lib/core/iterable.dart b/sdk/lib/core/iterable.dart
index 83618ce3c2f3936a9c59450dad520476f0829182..0fda6f168210abd481b330fcab6e19f422e91fc1 100644
--- a/sdk/lib/core/iterable.dart
+++ b/sdk/lib/core/iterable.dart
@@ -98,12 +98,31 @@ abstract class Iterable<E> {
* Use [initialValue] as the initial value, and the function [combine] to
* create a new value from the previous one and an element.
*
- * Example of calculating the sum of a collection:
+ * Example of calculating the sum of an iterable:
*
- * collection.reduce(0, (prev, element) => prev + element);
+ * iterable.reduce((prev, element) => prev + element);
+ *
+ * *UPCOMING API-CHANGE*: this method will soon be changed to not take
+ * an initial value: `iterable.reduce(min)`. Use [fold] instead.
*/
+ @deprecated
dynamic reduce(var initialValue,
dynamic combine(var previousValue, E element)) {
+ return fold(initialValue, combine);
+ }
+
+ /**
+ * Reduce a collection to a single value by iteratively combining each element
+ * of the collection with an existing value using the provided function.
+ * Use [initialValue] as the initial value, and the function [combine] to
+ * create a new value from the previous one and an element.
+ *
+ * Example of calculating the sum of an iterable:
+ *
+ * iterable.fold(0, (prev, element) => prev + element);
+ */
+ dynamic fold(var initialValue,
+ dynamic combine(var previousValue, E element)) {
var value = initialValue;
for (E element in this) value = combine(value, element);
return value;
« no previous file with comments | « sdk/lib/collection/collections.dart ('k') | sdk/lib/html/dart2js/html_dart2js.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698