Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 part of dart.core; | 5 part of dart.core; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * The [Iterable] interface allows to get an [Iterator] out of an | 8 * The [Iterable] interface allows to get an [Iterator] out of an |
| 9 * [Iterable] object. | 9 * [Iterable] object. |
| 10 * | 10 * |
| 11 * This interface is used by the for-in construct to iterate over an | 11 * This interface is used by the for-in construct to iterate over an |
| 12 * [Iterable] object. | 12 * [Iterable] object. |
| 13 * The for-in construct takes an [Iterable] object at the right-hand | 13 * The for-in construct takes an [Iterable] object at the right-hand |
| 14 * side, and calls its [iterator] method to get an [Iterator] on it. | 14 * side, and calls its [iterator] method to get an [Iterator] on it. |
| 15 * | 15 * |
| 16 * A user-defined class that implements the [Iterable] interface can | 16 * A user-defined class that implements the [Iterable] interface can |
| 17 * be used as the right-hand side of a for-in construct. | 17 * be used as the right-hand side of a for-in construct. |
| 18 */ | 18 */ |
| 19 abstract class Iterable<E> { | 19 abstract class Iterable<E> { |
| 20 const Iterable(); | 20 const Iterable(); |
| 21 | 21 |
| 22 factory Iterable.generate(int count, E generator(int index)) { | |
|
Sean Eagan
2013/01/09 14:15:20
Once we have a some way to get a integer range, I
| |
| 23 return new _GeneratorIterable<E>(count, generator); | |
| 24 } | |
| 25 | |
| 22 /** | 26 /** |
| 23 * Returns an [Iterator] that iterates over this [Iterable] object. | 27 * Returns an [Iterator] that iterates over this [Iterable] object. |
| 24 */ | 28 */ |
| 25 Iterator<E> get iterator; | 29 Iterator<E> get iterator; |
| 26 | 30 |
| 27 /** | 31 /** |
| 28 * Returns a lazy [Iterable] where each element [:e:] of [this] is replaced | 32 * Returns a lazy [Iterable] where each element [:e:] of [this] is replaced |
| 29 * by the result of [:f(e):]. | 33 * by the result of [:f(e):]. |
| 30 * | 34 * |
| 31 * This method returns a view of the mapped elements. As long as the | 35 * This method returns a view of the mapped elements. As long as the |
| (...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 581 _hasSkipped = true; | 585 _hasSkipped = true; |
| 582 while (_iterator.moveNext()) { | 586 while (_iterator.moveNext()) { |
| 583 if (!_f(_iterator.current)) return true; | 587 if (!_f(_iterator.current)) return true; |
| 584 } | 588 } |
| 585 } | 589 } |
| 586 return _iterator.moveNext(); | 590 return _iterator.moveNext(); |
| 587 } | 591 } |
| 588 | 592 |
| 589 E get current => _iterator.current; | 593 E get current => _iterator.current; |
| 590 } | 594 } |
| 595 | |
| 596 | |
| 597 typedef E _Generator<E>(int index); | |
| 598 | |
| 599 class _GeneratorIterable<E> extends Iterable<E> { | |
| 600 final int _count; | |
| 601 final _Generator<E> _generator; | |
| 602 _GeneratorIterable(this._count, this._generator); | |
| 603 Iterator<E> get iterator => new _GeneratorIterator(_count, _generator); | |
| 604 } | |
| 605 | |
| 606 class _GeneratorIterator<E> implements Iterator<E> { | |
| 607 final int _count; | |
| 608 final _Generator<E> _generator; | |
| 609 int _index = 0; | |
| 610 E _current; | |
| 611 | |
| 612 _GeneratorIterator(this._count, this._generator); | |
| 613 | |
| 614 bool moveNext() { | |
| 615 if (_index < _count) { | |
| 616 _current = _generator(_index); | |
| 617 _index++; | |
| 618 return true; | |
| 619 } else { | |
| 620 _current = null; | |
| 621 return false; | |
| 622 } | |
| 623 } | |
| 624 | |
| 625 E get current => _current; | |
| 626 } | |
| OLD | NEW |