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

Unified Diff: sdk/lib/core/iterable.dart

Issue 11794044: Add Stream.fromIterable (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Added the test file too. 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
Index: sdk/lib/core/iterable.dart
diff --git a/sdk/lib/core/iterable.dart b/sdk/lib/core/iterable.dart
index 083a86db0cfd78a7869f5a9b88f97fcd08fba541..74b78c3232c5adf40ed0f67c54bce6d429c1d3c4 100644
--- a/sdk/lib/core/iterable.dart
+++ b/sdk/lib/core/iterable.dart
@@ -19,6 +19,9 @@ part of dart.core;
abstract class Iterable<E> {
const Iterable();
+ factory Iterable.generate(int count, E generator(int index))
+ = _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.
+
/**
* Returns an [Iterator] that iterates over this [Iterable] object.
*/
@@ -572,3 +575,35 @@ class SkipWhileIterator<E> extends Iterator<E> {
E get current => _iterator.current;
}
+
+
+typedef E _Generator<E>(int index);
+
+class _GeneratorIterable<E> extends Iterable<E> {
+ int _count;
+ _Generator<E> _generator;
floitsch 2013/01/08 13:02:54 final
Lasse Reichstein Nielsen 2013/01/09 11:27:36 Done.
+ _GeneratorIterable(this._count, this._generator);
+ Iterable<E> get iterator => new _GeneratorIterator(_count, _generator);
+}
+
+class _GeneratorIterator<E> implements Iterator<E> {
+ final int _count;
+ int _index = 0;
+ final _Generator<E> _generator;
+ E _current;
+
+ _GeneratorIterator(this._count, this._generator);
+
+ bool moveNext() {
+ if (_index < _count) {
+ _current = _generator(_index);
+ _index++;
+ return true;
+ } else {
+ _current = null;
+ return false;
+ }
+ }
+
+ E get current => _current;
+}

Powered by Google App Engine
This is Rietveld 408576698