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

Side by Side Diff: sdk/lib/collection_dev/iterable.dart

Issue 12188011: Added expand method to iterables. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/collection/collections.dart ('k') | sdk/lib/core/iterable.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.collection.dev; 5 part of dart.collection.dev;
6 6
7 typedef T _Transformation<S, T>(S value); 7 typedef T _Transformation<S, T>(S value);
8 8
9 class MappedIterable<S, T> extends Iterable<T> { 9 class MappedIterable<S, T> extends Iterable<T> {
10 final Iterable<S> _iterable; 10 final Iterable<S> _iterable;
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 if (_f(_iterator.current)) { 398 if (_f(_iterator.current)) {
399 return true; 399 return true;
400 } 400 }
401 } 401 }
402 return false; 402 return false;
403 } 403 }
404 404
405 E get current => _iterator.current; 405 E get current => _iterator.current;
406 } 406 }
407 407
408 typedef Iterable<T> _ExpandFunction<S, T>(S sourceElement);
409
410 class ExpandIterable<S, T> extends Iterable<T> {
411 final Iterable<S> _iterable;
412 // TODO(ahe): Restore type when feature is implemented in dart2js
413 // checked mode. http://dartbug.com/7733
414 final /* _ExpandFunction */ _f;
415
416 ExpandIterable(this._iterable, Iterable<T> this._f(S element));
417
418 Iterator<T> get iterator => new ExpandIterator<S, T>(_iterable.iterator, _f);
419 }
420
421 class ExpandIterator<S, T> implements Iterator<T> {
422 final Iterator<S> _iterator;
423 // TODO(ahe): Restore type when feature is implemented in dart2js
424 // checked mode. http://dartbug.com/7733
425 final /* _ExpandFunction */ _f;
426 // Initialize _currentExpansion to an empty iterable. A null value
427 // marks the end of iteration, and we don't want to call _f before
428 // the first moveNext call.
429 Iterator<T> _currentExpansion = const EmptyIterator();
430 T _current;
431
432 ExpandIterator(this._iterator, Iterable<T> this._f(S element));
433
434 void _nextExpansion() {
435 }
436
437 T get current => _current;
438
439 bool moveNext() {
440 if (_currentExpansion == null) return false;
441 while (!_currentExpansion.moveNext()) {
442 _current = null;
443 if (_iterator.moveNext()) {
444 // If _f throws, this ends iteration. Otherwise _currentExpansion and
445 // _current will be set again below.
446 _currentExpansion = null;
447 _currentExpansion = _f(_iterator.current).iterator;
448 } else {
449 return false;
450 }
451 }
452 _current = _currentExpansion.current;
453 return true;
454 }
455 }
456
408 class TakeIterable<E> extends Iterable<E> { 457 class TakeIterable<E> extends Iterable<E> {
409 final Iterable<E> _iterable; 458 final Iterable<E> _iterable;
410 final int _takeCount; 459 final int _takeCount;
411 460
412 TakeIterable(this._iterable, this._takeCount) { 461 TakeIterable(this._iterable, this._takeCount) {
413 if (_takeCount is! int || _takeCount < 0) { 462 if (_takeCount is! int || _takeCount < 0) {
414 throw new ArgumentError(_takeCount); 463 throw new ArgumentError(_takeCount);
415 } 464 }
416 } 465 }
417 466
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
623 672
624 Set toSet() => new Set<E>(); 673 Set toSet() => new Set<E>();
625 } 674 }
626 675
627 /** The always empty iterator. */ 676 /** The always empty iterator. */
628 class EmptyIterator<E> implements Iterator<E> { 677 class EmptyIterator<E> implements Iterator<E> {
629 const EmptyIterator(); 678 const EmptyIterator();
630 bool moveNext() => false; 679 bool moveNext() => false;
631 E get current => null; 680 E get current => null;
632 } 681 }
OLDNEW
« no previous file with comments | « sdk/lib/collection/collections.dart ('k') | sdk/lib/core/iterable.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698