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

Unified Diff: pkg/polymer_expressions/lib/src/globals.dart

Issue 23874018: add enumerate to the default filter set (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 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 | « pkg/polymer_expressions/lib/polymer_expressions.dart ('k') | pkg/polymer_expressions/lib/src/mirrors.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/polymer_expressions/lib/src/globals.dart
diff --git a/pkg/polymer_expressions/lib/src/globals.dart b/pkg/polymer_expressions/lib/src/globals.dart
new file mode 100644
index 0000000000000000000000000000000000000000..86a19d8522a87468e82c88f2da47d1d5a5777ad2
--- /dev/null
+++ b/pkg/polymer_expressions/lib/src/globals.dart
@@ -0,0 +1,72 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+/**
+ * Contains functions that are included by default in [PolymerExpressions].
+ *
+ * - [enumerate]: a convenient way to iterate over items and the indexes.
+ */
+// Code from https://github.com/google/quiver-dart/commit/52edc4baf37e99ff6a8f99c648b29b135fc0b880
+library polymer_expressions.src.globals;
+
+import 'dart:collection';
+
+/**
+ * Returns an [Iterable] of [IndexedValue]s where the nth value holds the nth
+ * element of [iterable] and its index.
+ */
+Iterable<IndexedValue> enumerate(Iterable iterable) =>
+ new EnumerateIterable(iterable);
+
+class IndexedValue<V> {
+ final int index;
+ final V value;
+
+ IndexedValue(this.index, this.value);
+}
+
+
+/**
+ * An [Iterable] of [IndexedValue]s where the nth value holds the nth
+ * element of [iterable] and its index. See [enumerate].
+ */
+// This was inspired by MappedIterable internal to Dart collections.
+class EnumerateIterable<V> extends IterableBase<IndexedValue<V>> {
+ final Iterable<V> _iterable;
+
+ EnumerateIterable(this._iterable);
+
+ Iterator<V> get iterator => new EnumerateIterator<V>(_iterable.iterator);
+
+ // Length related functions are independent of the mapping.
+ int get length => _iterable.length;
+ bool get isEmpty => _iterable.isEmpty;
+
+ // Index based lookup can be done before transforming.
+ IndexedValue<V> get first => new IndexedValue<V>(0, _iterable.first);
+ IndexedValue<V> get last => new IndexedValue<V>(length - 1, _iterable.last);
+ IndexedValue<V> get single => new IndexedValue<V>(0, _iterable.single);
+ IndexedValue<V> elementAt(int index) =>
+ new IndexedValue<V>(index, _iterable.elementAt(index));
+}
+
+/** The [Iterator] returned by [EnumerateIterable.iterator]. */
+class EnumerateIterator<V> extends Iterator<IndexedValue<V>> {
+ final Iterator<IndexedValue<V>> _iterator;
+ int _index = 0;
+ IndexedValue<V> _current;
+
+ EnumerateIterator(this._iterator);
+
+ IndexedValue<V> get current => _current;
+
+ bool moveNext() {
+ if (_iterator.moveNext()) {
+ _current = new IndexedValue(_index++, _iterator.current);
+ return true;
+ }
+ _current = null;
+ return false;
+ }
+}
« no previous file with comments | « pkg/polymer_expressions/lib/polymer_expressions.dart ('k') | pkg/polymer_expressions/lib/src/mirrors.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698