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

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: Address comments. Fix bug hit by new test. 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
« no previous file with comments | « sdk/lib/async/stream_impl.dart ('k') | sdk/lib/isolate/isolate_stream.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/core/iterable.dart
diff --git a/sdk/lib/core/iterable.dart b/sdk/lib/core/iterable.dart
index feb93184746be1d33a743102af8326e94241ccb8..a2cc9cd2eb74d7c8d86f0e804df5408e309f773a 100644
--- a/sdk/lib/core/iterable.dart
+++ b/sdk/lib/core/iterable.dart
@@ -19,6 +19,10 @@ part of dart.core;
abstract class Iterable<E> {
const Iterable();
+ 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
+ return new _GeneratorIterable<E>(count, generator);
+ }
+
/**
* Returns an [Iterator] that iterates over this [Iterable] object.
*/
@@ -588,3 +592,35 @@ class SkipWhileIterator<E> extends Iterator<E> {
E get current => _iterator.current;
}
+
+
+typedef E _Generator<E>(int index);
+
+class _GeneratorIterable<E> extends Iterable<E> {
+ final int _count;
+ final _Generator<E> _generator;
+ _GeneratorIterable(this._count, this._generator);
+ Iterator<E> get iterator => new _GeneratorIterator(_count, _generator);
+}
+
+class _GeneratorIterator<E> implements Iterator<E> {
+ final int _count;
+ final _Generator<E> _generator;
+ int _index = 0;
+ 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;
+}
« no previous file with comments | « sdk/lib/async/stream_impl.dart ('k') | sdk/lib/isolate/isolate_stream.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698