Skip to content

Instantly share code, notes, and snippets.

@grondilu
Created December 7, 2012 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grondilu/4231872 to your computer and use it in GitHub Desktop.
Save grondilu/4231872 to your computer and use it in GitHub Desktop.
perl6 Set operators
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