Skip to content

Instantly share code, notes, and snippets.

@kevmoo
Last active January 4, 2016 20:10
Show Gist options
  • Save kevmoo/8672644 to your computer and use it in GitHub Desktop.
Save kevmoo/8672644 to your computer and use it in GitHub Desktop.
Lazy Iterable
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