Created
January 10, 2012 08:39
-
-
Save TimToady/1587894 to your computer and use it in GitHub Desktop.
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
class Set is Iterable does Associative; | |
has Bool %!elems[Any] handles <keys values elems exists Bool Numeric hash>; | |
constant term:<∅> = set(); | |
# Immutable | |
method !STORE(\args) { | |
die 'Sets are immutable, but you tried to modify one' | |
} | |
# Constructor | |
sub set(*@args) is export { | |
Set.bless(self.CREATE, :elems( EnumMap.new(@elems X=> True )); | |
} | |
# Coercions to and from | |
multi method &.() (Set $set) { $set } | |
multi method &.() (@elems) { set @elems } | |
multi method &.() (%elems) { set %elems.keys } | |
multi method &.() ($elem) { fail "Cannot coerce $elem.perl() to a Set; use set($elem.perl()) to create a one-element set" } | |
method Str() { "set(< %!elems.keys() >)" } | |
method gist() { "set({ %!elems.keys».gist.join(', ') })" } | |
method perl() { 'set(' ~ join(', ', map { .perl }, %!elems.keys) ~ ')' } | |
method iterator() { %!elems.keys.iterator } | |
method list() { %!elems.keys } | |
method pick(\args) { %!elems.keys.pick: |args } | |
method roll(\args) { %!elems.keys.roll: |args } | |
# Set operators | |
multi sub infix:<∈>($a, Any $b) is export { $a ∈ Set($b) } | |
multi sub infix:<∈>($a, Set $b) is export { ?$b{$a} } | |
multi sub infix:<∉>($a, $b) is export { $a !∈ $b } | |
multi sub infix:<∋>($a, Any $b) is export { Set($a) ∋ $b } | |
multi sub infix:<∋>($a, Set $b) is export { ?$a{$b} } | |
multi sub infix:<∌>($a, $b) is export { $a !∋ $b } | |
multi sub infix:<∪>(Any $a, Any $b) is export { Set($a) ∪ Set($b) } | |
multi sub infix:<∪>(Set $a, Set $b) is export { set $a.keys, $b.keys } | |
multi sub infix:<(|)>($a, $b) is export { $a ∪ $b } | |
multi sub infix:<∩>(Any $a, Any $b) is export { Set($a) ∩ Set($b) } | |
multi sub infix:<∩>(Set $a, Set $b) is export { set $a.keys.grep: * ∈ $b } | |
multi sub infix:<(&)>($a, $b) is export { $a ∩ $b } | |
multi sub infix:<(-)>(Any $a, Any $b) is export { Set($a) (-) Set($b) } | |
multi sub infix:<(-)>(Set $a, Set $b) is export { set $a.keys.grep: * ∉ $b } | |
multi sub infix:<(^)>(Any $a, Any $b) is export { Set($a) (^) Set($b) } | |
multi sub infix:<(^)>(Set $a, Set $b) is export { ($a (-) $b) ∪ ($b (-) $a) } | |
multi sub infix:<===>(Any $a, Any $b) is export { Set($a) === Set($b) } | |
multi sub infix:<===>(Set $a, Set $b) is export { $a == $b and so $a.keys.all ∈ $b } | |
multi sub infix:<eqv>(Any $a, Any $b) is export { Set($a) eqv Set($b) } | |
multi sub infix:<eqv>(Set $a, Set $b) is export { $a == $b and so $a.keys.all ∈ $b } | |
multi sub infix:<⊆>(Any $a, Any $b) is export { Set($a) ⊆ Set($b) } | |
multi sub infix:<⊆>(Set $a, Set $b) is export { $a <= $b and so $a.keys.all ∈ $b } | |
multi sub infix:['(<=)']($a, $b) is export { $a ⊆ $b } | |
multi sub infix:<⊈>($a, $b) is export { $a !⊆ $b } | |
multi sub infix:<⊂>(Any $a, Any $b) is export { Set($a) ⊂ Set($b) } | |
multi sub infix:<⊂>(Set $a, Set $b) is export { $a < $b and so $a.keys.all ∈ $b } | |
multi sub infix:['(<)']($a, $b) is export { $a ⊂ $b } | |
multi sub infix:<⊄>($a, $b) is export { $a !⊂ $b } | |
multi sub infix:<⊇>(Any $a, Any $b) is export { Set($a) ⊇ Set($b) } | |
multi sub infix:<⊇>(Set $a, Set $b) is export { $a >= $b and so $b.keys.all ∈ $a } | |
multi sub infix:['(>=)']($a, $b) is export { $a ⊇ $b } | |
multi sub infix:<⊉>($a, $b) is export { $a !⊇ $b } | |
multi sub infix:<⊃>(Any $a, Any $b) is export { Set($a) ⊃ Set($b) } | |
multi sub infix:<⊃>(Set $a, Set $b) is export { $a > $b and so $b.keys.all ∈ $a } | |
multi sub infix:['(>)']($a, $b) is export { $a ⊃ $b } | |
multi sub infix:<⊅>($a, $b) is export { $a !⊃ $b } | |
# vim: ft=perl6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment