Created
October 29, 2021 09:13
-
-
Save Kavantix/db35f50bd65dd762bfb431a9f43edb0f to your computer and use it in GitHub Desktop.
Dart view of a list with a map function
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'; | |
abstract class MappedListView<T, R> implements List<R> { | |
factory MappedListView( | |
List<T> wrapped, | |
R Function(T element) mapper, | |
) = _MappedListView<T, R>; | |
} | |
class _MappedListView<T, R> with ListMixin<R> implements List<R>, MappedListView<T, R> { | |
_MappedListView( | |
this.wrapped, | |
this.mapper, | |
); | |
final List<T> wrapped; | |
final R Function(T element) mapper; | |
@override | |
int get length => wrapped.length; | |
@override | |
set length(int value) => wrapped.length = value; | |
@override | |
R operator [](int index) { | |
return mapper(wrapped[index]); | |
} | |
@override | |
void operator []=(int index, R value) { | |
throw UnsupportedError('Assigning to a MappedListView is not supported'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment