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

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: Created 7 years, 9 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
Index: sdk/lib/core/iterable.dart
diff --git a/sdk/lib/core/iterable.dart b/sdk/lib/core/iterable.dart
index 604c4a88940ff9bbe8cf1dc08cd88ded2e160531..65bd21ef71edef9d42596744329ebde1eb474ab7 100644
--- a/sdk/lib/core/iterable.dart
+++ b/sdk/lib/core/iterable.dart
@@ -98,12 +98,30 @@ 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.
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.
*/
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;

Powered by Google App Engine
This is Rietveld 408576698