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

Unified Diff: sdk/lib/collection/collections.dart

Issue 14071002: Added new version of reduce. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Removed more uses of max, and a few bugs. 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
Index: sdk/lib/collection/collections.dart
diff --git a/sdk/lib/collection/collections.dart b/sdk/lib/collection/collections.dart
index f51d98daaabeb87b5ab5bb1613a0f12e4fd5cc6e..95d5a43dedf80fdd0e63f73b14b3e08c63a2b94f 100644
--- a/sdk/lib/collection/collections.dart
+++ b/sdk/lib/collection/collections.dart
@@ -39,9 +39,14 @@ class IterableMixinWorkaround {
}
static dynamic reduce(Iterable iterable,
- dynamic initialValue,
- dynamic combine(dynamic previousValue, element)) {
- return fold(iterable, initialValue, combine);
+ dynamic combine(previousValue, element)) {
+ Iterator iterator = iterable.iterator;
+ if (!iterator.moveNext()) throw new StateError("No elements");
+ var value = iterator.current;
+ while (iterator.moveNext()) {
+ value = combine(value, iterator.current);
+ }
+ return value;
}
static dynamic fold(Iterable iterable,

Powered by Google App Engine
This is Rietveld 408576698