perl6 Set operators
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
| use v6; | |
| my @data = '10', '{1, 2, 3, 4, 5}', '{2, 8, 5, 10}'; | |
| my Set $N .= new: 1 .. @data.shift.Int; | |
| my Set $A .= new: eval @data.shift.subst(/<[{}]>/, '', :g); | |
| my Set $B .= new: eval @data.shift.subst(/<[{}]>/, '', :g); | |
| sub infix:<∩>(Set \A, Set \B) { Set.new: B.list.grep: * eq any A.list } | |
| sub infix:<∪>(Set \A, Set \B) { Set.new: |$A.list, |$B.list } | |
| sub infix:<->(Set \A, Set \B) { Set.new: grep none(B.list), A.list } | |
| sub prefix:<not>(Set \A) { $N - A } | |
| sub show(Set \A) { say '{', A.list.join(', '), '}' } | |
| show $A ∪ $B; | |
| show $A ∩ $B; | |
| show $A - $B; | |
| show $B - $A; | |
| show not $A; | |
| show not $B; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment