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

Unified Diff: samples/swarm/swarm_ui_lib/util/CollectionUtils.dart

Issue 11783009: Big merge from experimental to bleeding edge. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 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 | « samples/swarm/swarm_ui_lib/touch/touch.dart ('k') | samples/swarm/swarm_ui_lib/view/CompositeView.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: samples/swarm/swarm_ui_lib/util/CollectionUtils.dart
diff --git a/samples/swarm/swarm_ui_lib/util/CollectionUtils.dart b/samples/swarm/swarm_ui_lib/util/CollectionUtils.dart
index ca8e0191a60a3bf55e58c9332feab36a9ed7a347..763738b6ae4997c24062a2a08ad83b421d676cee 100644
--- a/samples/swarm/swarm_ui_lib/util/CollectionUtils.dart
+++ b/samples/swarm/swarm_ui_lib/util/CollectionUtils.dart
@@ -40,22 +40,6 @@ class CollectionUtils {
}
}
- // Collection<T> supports most of the ES 5 list methods, but it's missing
- // map.
-
- // TODO(jmesserly): we might want a version of this that return an iterable,
- // however JS, Python and Ruby versions are all eager.
- static List map(Iterable source, var mapper) {
- // TODO(jmesserly): I was trying to set the capacity here, but instead it
- // seems to create a fixed list. Hence assigning by index below.
- List result = new List(source is List ? (source as List).length : null);
- int i = 0;
- for (final item in source) {
- result[i++] = mapper(item);
- }
- return result;
- }
-
/**
* Finds the item in [source] that matches [test]. Returns null if
* no item matches. The typing should be:
@@ -72,26 +56,26 @@ class CollectionUtils {
/** Compute the minimum of an iterable. Returns null if empty. */
static num min(Iterable source) {
- final iter = source.iterator();
- if (!iter.hasNext) {
+ final iter = source.iterator;
+ if (!iter.moveNext()) {
return null;
}
- num best = iter.next();
- while (iter.hasNext) {
- best = Math.min(best, iter.next());
+ num best = iter.current;
+ while (iter.moveNext()) {
+ best = Math.min(best, iter.current);
}
return best;
}
/** Compute the maximum of an iterable. Returns null if empty. */
static num max(Iterable source) {
- final iter = source.iterator();
- if (!iter.hasNext) {
+ final iter = source.iterator;
+ if (!iter.moveNext()) {
return null;
}
- num best = iter.next();
- while (iter.hasNext) {
- best = Math.max(best, iter.next());
+ num best = iter.current;
+ while (iter.moveNext()) {
+ best = Math.max(best, iter.current);
}
return best;
}
@@ -117,17 +101,21 @@ class CollectionUtils {
/** Compute the sum of an iterable. An empty iterable is an error. */
static num sum(Iterable source, [NumericValueSelector selector = null]) {
- final iter = source.iterator();
+ final iter = source.iterator;
+ bool wasEmpty = true;
num total = 0;
if (selector != null) {
- do {
- total += selector(iter.next());
- } while (iter.hasNext);
+ for (var element in source) {
+ wasEmpty = false;
+ total += selector(element);
+ }
} else {
- do {
- total += iter.next();
- } while (iter.hasNext);
+ for (num element in source) {
+ wasEmpty = false;
+ total += element;
+ }
}
+ if (wasEmpty) throw new StateError("No elements");
return total;
}
« no previous file with comments | « samples/swarm/swarm_ui_lib/touch/touch.dart ('k') | samples/swarm/swarm_ui_lib/view/CompositeView.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698