Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Contains functions that are included by default in [PolymerExpressions]. | 6 * Contains functions that are included by default in [PolymerExpressions]. |
| 7 * | 7 * |
| 8 * - [enumerate]: a convenient way to iterate over items and the indexes. | 8 * - [enumerate]: a convenient way to iterate over items and the indexes. |
| 9 */ | 9 */ |
| 10 // Code from https://github.com/google/quiver-dart/commit/52edc4baf37e99ff6a8f99 c648b29b135fc0b880 | 10 // Code from https://github.com/google/quiver-dart/commit/52edc4baf37e99ff6a8f99 c648b29b135fc0b880 |
| 11 library polymer_expressions.src.globals; | 11 library polymer_expressions.src.globals; |
| 12 | 12 |
| 13 import 'dart:collection'; | 13 import 'dart:collection'; |
| 14 import 'package:observe/observe.dart' show reflectable; | 14 import 'package:observe/observe.dart' show reflectable; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Returns an [Iterable] of [IndexedValue]s where the nth value holds the nth | 17 * Returns an [Iterable] of [IndexedValue]s where the nth value holds the nth |
| 18 * element of [iterable] and its index. | 18 * element of [iterable] and its index. |
| 19 */ | 19 */ |
| 20 Iterable<IndexedValue> enumerate(Iterable iterable) => | 20 Iterable<IndexedValue> enumerate(Iterable iterable) => |
| 21 new EnumerateIterable(iterable); | 21 new EnumerateIterable(iterable); |
| 22 | 22 |
| 23 @reflectable class IndexedValue<V> { | 23 @reflectable class IndexedValue<V> { |
| 24 final int index; | 24 final int index; |
| 25 final V value; | 25 final V value; |
| 26 | 26 |
| 27 operator==(o) => o is IndexedValue && o.index == index && o.value == value; | |
|
Jennifer Messerly
2013/11/14 02:50:49
wonder if we should upstream this (if you haven't
Siggi Cherem (dart-lang)
2013/11/14 17:56:11
good idea. Will do
| |
| 28 int get hashCode => value.hashCode; | |
| 29 String toString() => '($index, $value)'; | |
| 30 | |
| 27 IndexedValue(this.index, this.value); | 31 IndexedValue(this.index, this.value); |
| 28 } | 32 } |
| 29 | 33 |
| 30 /** | 34 /** |
| 31 * An [Iterable] of [IndexedValue]s where the nth value holds the nth | 35 * An [Iterable] of [IndexedValue]s where the nth value holds the nth |
| 32 * element of [iterable] and its index. See [enumerate]. | 36 * element of [iterable] and its index. See [enumerate]. |
| 33 */ | 37 */ |
| 34 // This was inspired by MappedIterable internal to Dart collections. | 38 // This was inspired by MappedIterable internal to Dart collections. |
| 35 class EnumerateIterable<V> extends IterableBase<IndexedValue<V>> { | 39 class EnumerateIterable<V> extends IterableBase<IndexedValue<V>> { |
| 36 final Iterable<V> _iterable; | 40 final Iterable<V> _iterable; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 64 | 68 |
| 65 bool moveNext() { | 69 bool moveNext() { |
| 66 if (_iterator.moveNext()) { | 70 if (_iterator.moveNext()) { |
| 67 _current = new IndexedValue(_index++, _iterator.current); | 71 _current = new IndexedValue(_index++, _iterator.current); |
| 68 return true; | 72 return true; |
| 69 } | 73 } |
| 70 _current = null; | 74 _current = null; |
| 71 return false; | 75 return false; |
| 72 } | 76 } |
| 73 } | 77 } |
| OLD | NEW |