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)) | |
| 23 = _GeneratorIterable; | |
|
floitsch
2013/01/08 13:02:54
Add TODO that we need to forward the generic type.
Lasse Reichstein Nielsen
2013/01/09 11:27:36
My mistake, I thought it was implicitly forwarded.
| |
| 24 | |
| 22 /** | 25 /** |
| 23 * Returns an [Iterator] that iterates over this [Iterable] object. | 26 * Returns an [Iterator] that iterates over this [Iterable] object. |
| 24 */ | 27 */ |
| 25 Iterator<E> get iterator; | 28 Iterator<E> get iterator; |
| 26 | 29 |
| 27 /** | 30 /** |
| 28 * Returns a lazy [Iterable] where each element [:e:] of [this] is replaced | 31 * Returns a lazy [Iterable] where each element [:e:] of [this] is replaced |
| 29 * by the result of [:f(e):]. | 32 * by the result of [:f(e):]. |
| 30 * | 33 * |
| 31 * This method returns a view of the mapped elements. As long as the | 34 * This method returns a view of the mapped elements. As long as the |
| (...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 565 _hasSkipped = true; | 568 _hasSkipped = true; |
| 566 while (_iterator.moveNext()) { | 569 while (_iterator.moveNext()) { |
| 567 if (!_f(_iterator.current)) return true; | 570 if (!_f(_iterator.current)) return true; |
| 568 } | 571 } |
| 569 } | 572 } |
| 570 return _iterator.moveNext(); | 573 return _iterator.moveNext(); |
| 571 } | 574 } |
| 572 | 575 |
| 573 E get current => _iterator.current; | 576 E get current => _iterator.current; |
| 574 } | 577 } |
| 578 | |
| 579 | |
| 580 typedef E _Generator<E>(int index); | |
| 581 | |
| 582 class _GeneratorIterable<E> extends Iterable<E> { | |
| 583 int _count; | |
| 584 _Generator<E> _generator; | |
|
floitsch
2013/01/08 13:02:54
final
Lasse Reichstein Nielsen
2013/01/09 11:27:36
Done.
| |
| 585 _GeneratorIterable(this._count, this._generator); | |
| 586 Iterable<E> get iterator => new _GeneratorIterator(_count, _generator); | |
| 587 } | |
| 588 | |
| 589 class _GeneratorIterator<E> implements Iterator<E> { | |
| 590 final int _count; | |
| 591 int _index = 0; | |
| 592 final _Generator<E> _generator; | |
| 593 E _current; | |
| 594 | |
| 595 _GeneratorIterator(this._count, this._generator); | |
| 596 | |
| 597 bool moveNext() { | |
| 598 if (_index < _count) { | |
| 599 _current = _generator(_index); | |
| 600 _index++; | |
| 601 return true; | |
| 602 } else { | |
| 603 _current = null; | |
| 604 return false; | |
| 605 } | |
| 606 } | |
| 607 | |
| 608 E get current => _current; | |
| 609 } | |
| OLD | NEW |