Last active
September 15, 2022 08:31
-
-
Save liudonghua123/bcae09cf830c20ec99e7868b644fa7ff to your computer and use it in GitHub Desktop.
add some operator override on Set date type
This file contains hidden or 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
// 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