Skip to content

Instantly share code, notes, and snippets.

@liudonghua123
Last active September 15, 2022 08:31
Show Gist options
  • Save liudonghua123/bcae09cf830c20ec99e7868b644fa7ff to your computer and use it in GitHub Desktop.
Save liudonghua123/bcae09cf830c20ec99e7868b644fa7ff to your computer and use it in GitHub Desktop.
add some operator override on Set date type
// https://www.geeksforgeeks.org/python-set-operations-union-intersection-difference-symmetric-difference/
// https://dart.cn/guides/language/extension-methods#implementing-generic-extensions
// https://api.dart.cn/stable/2.18.1/dart-core/Set-class.html
extension EhancedSet<T> on Set<T> {
Set<T> operator |(Set<T> anotherSet) => union(anotherSet);
Set<T> operator &(Set<T> anotherSet) => intersection(anotherSet);
Set<T> operator -(Set<T> anotherSet) => difference(anotherSet);
Set<T> operator ^(Set<T> anotherSet) => union(anotherSet).difference(intersection(anotherSet));
}
void main() {
var gfg1 = <String>{"GeeksForGeeks", "Geek1", "Geek2", "Geek3"};
var gfg2 = <String>{"GeeksForGeeks", "Geek3", "Geek4", "Geek5"};
print(gfg1 | gfg2);
print(gfg1 & gfg2);
print(gfg1 - gfg2);
print(gfg1 ^ gfg2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment