Last active
December 15, 2015 11:49
-
-
Save thara/5255354 to your computer and use it in GitHub Desktop.
Mix-in sample in Dart.
I want to make Seq looks like Seq in Scala...
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 'package:unittest/unittest.dart'; | |
void main() { | |
print("intersect"); | |
(new Seq([1, 2, 3]) & new Seq([2, 3, 4])).forEach(print); | |
print("union"); | |
(new Seq([1, 2, 3]) | new Seq([2, 3, 4])).forEach(print); | |
/* | |
(you will get the following in your console) | |
intersect | |
2 | |
3 | |
union | |
1 | |
2 | |
3 | |
4 | |
*/ | |
} | |
abstract class Operatable<E> implements List<E> { | |
Operatable<E> newInstance(); | |
Operatable<E> operator &(Operatable<E> other) { | |
Operatable<E> result = newInstance(); | |
result.addAll(this.where((e) => other.contains(e))); | |
return result; | |
} | |
Operatable<E> operator |(Operatable<E> other) { | |
// Terrible...Any idea? | |
Set<E> set = new Set<E>(); | |
set.addAll(this); | |
set.addAll(other); | |
Operatable<E> result = newInstance(); | |
result.addAll(set); | |
return result; | |
} | |
} | |
class Seq<E> extends ListBase<E> with Operatable<E> { | |
List<E> list; | |
Seq([this.list]); | |
int get length => list.length; | |
void set length(int newLength) { | |
list.length = newLength; | |
} | |
Iterator<E> get iterator => list.iterator; | |
E operator [](int index) => list[index]; | |
operator []=(int index, E value) { | |
list[index] = value; | |
} | |
Operatable<E> newInstance() { | |
return new Seq(<E>[]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment