, UnmodifiableListView:
import "package:unittest/unittest.dart";
import "dart:collection";
class Tuple<A,B> {
final A a;
final B b;
const Tuple(this.a, this.b);
String toString() {
return "(a: $a, b : $b)";
}
}
abstract class partitionMixin<RES, E>{
Iterable<E> where(bool test(E element));
Map<E, bool> _filterCache = new Map();
Tuple<RES,RES> partition(bool filter(E e)) {
bool cachedFilter(E e) {
if (_filterCache.containsKey(e)) return _filterCache[e];
else {
bool filterRes = filter(e);
_filterCache[e] = filterRes;
return filterRes;
}
}
return new Tuple(this.where(cachedFilter),
this.where((E e) => !cachedFilter(e)));
}
}
class ExtULV<E> = UnmodifiableListView<E> with
partitionMixin<ExtULV<E>,E>;
void main() {
test('Split one iterable in two"', () {
var foo = (e) => (e % 2) == 0;
var fooA = [2,4,6,8, 10, 12, 14];
var fooB = [1,3,5,7,9,11,13];
var fooRes = new Tuple(fooA, fooB);
var tested = new ExtULV([1,2,3,4,5,6,7,8,9,10,11,12,13,14]);
var testRes = tested.partition(foo);
print("fooRes : $fooRes\ntestRes: $testRes");
expect(testRes.a.toList().toString() == fooRes.a.toString(), true);
expect(testRes.b.toList().toString() == fooRes.b.toString(), true);
});
}