Last active
January 4, 2016 20:10
-
-
Save kevmoo/8672644 to your computer and use it in GitHub Desktop.
Lazy Iterable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:collection'; | |
void main() { | |
var list = new WatchList([1,2,3,4,5]); | |
var mapped = list.where((x) => x.isEven).map((x) => x*2); | |
// What is printed here? | |
print(list.accessCount); | |
var newList = list.where((x) => x.isEven).map((x) => x*2).toList(); | |
// What is printed here? | |
print(list.accessCount); | |
} | |
class WatchList<T> extends ListBase<T> { | |
final List<T> _innerList; | |
int _count = 0; | |
WatchList(List list) : _innerList = list; | |
int get accessCount => _count; | |
T operator[](int index) { | |
_count++; | |
return _innerList[index]; | |
} | |
int get length => _innerList.length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment