Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge Enum into Pair
This should reduce the confusion around enum, Enum an Enumeration. Pair now implements shallow immutability like List. Pairs themselves are immutable, but containers used as values can be assigned to.
- Loading branch information
Showing
17 changed files
with
104 additions
and
112 deletions.
There are no files selected for viewing
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,96 @@ | ||
| my class Pair is Enum { | ||
| method value() is rw { nqp::getattr(self, Enum, '$!value') } | ||
| my class Pair does Associative { | ||
| has $.key; | ||
| has $.value is rw; | ||
|
|
||
| multi method new($key, Mu \value) { | ||
| nqp::create(self).BUILD($key, value) | ||
| } | ||
| multi method new(:$key, Mu :$value) { | ||
| nqp::create(self).BUILD($key, $value) | ||
| } | ||
| method BUILD($!key, Mu \value) { | ||
| nqp::bindattr(self, Pair, '$!value', value); | ||
| self | ||
| } | ||
|
|
||
| multi method ACCEPTS(Pair:D: %h) { | ||
| $.value.ACCEPTS(%h{$.key}); | ||
| } | ||
|
|
||
| multi method ACCEPTS(Pair:D: Mu $other) { | ||
| $other."$.key"().Bool === $.value.Bool | ||
| } | ||
| multi method AT-KEY(Pair:D: $key) { $key eq nqp::getattr(self, Enum, '$!key') | ||
| ?? nqp::getattr(self, Enum, '$!value') | ||
| !! Any; | ||
|
|
||
| method antipair(Pair:D:) { self.new(key => $!value, value => $!key) } | ||
|
|
||
| multi method keys(Pair:D:) { ($!key,).list } | ||
| multi method kv(Pair:D:) { $!key, $!value } | ||
| multi method values(Pair:D:) { ($!value,).list } | ||
| multi method pairs(Pair:D:) { (self,).list } | ||
| multi method antipairs(Pair:D:) { self.new(key => $!value, value => $!key) } | ||
| multi method invert(Pair:D:) { $!value »=>» $!key } | ||
|
|
||
| multi method Str(Pair:D:) { $!key ~ "\t" ~ $!value } | ||
|
|
||
| multi method gist(Pair:D:) { | ||
| my $result; | ||
| if not %*gistseen<TOP> { my %*gistseen = :TOP ; return self.gist } | ||
| if %*gistseen{self.WHICH} { %*gistseen{self.WHICH} = 2; return "Pair_{self.WHERE}" } | ||
| %*gistseen{self.WHICH} = 1; | ||
| if nqp::istype($!key, Pair) { | ||
| $result = '(' ~ $!key.gist ~ ') => ' ~ $!value.gist; | ||
| } else { | ||
| $result = $!key.gist ~ ' => ' ~ $!value.gist; | ||
| } | ||
| $result = "(\\Pair_{self.WHERE} = $result)" if %*gistseen{self.WHICH}:delete == 2; | ||
| $result; | ||
| } | ||
|
|
||
| multi method perl(Pair:D: :$arglist) { | ||
| my $result; | ||
| if not %*perlseen<TOP> { my %*perlseen = :TOP ; return self.perl(:$arglist) } | ||
| if %*perlseen{self.WHICH} { %*perlseen{self.WHICH} = 2; return "Pair_{self.WHERE}" } | ||
| %*perlseen{self.WHICH} = 1; | ||
| if nqp::istype($!key, Pair) { | ||
| $result = '(' ~ $!key.perl ~ ') => ' ~ $!value.perl; | ||
| } elsif nqp::istype($!key, Str) and !$arglist and $!key ~~ /^ [<alpha>\w*] +% <[\-']> $/ { | ||
| if nqp::istype($!value,Bool) { | ||
| $result = ':' ~ '!' x !$!value ~ $!key; | ||
| } else { | ||
| $result = ':' ~ $!key ~ '(' ~ $!value.perl ~ ')'; | ||
| } | ||
| } else { | ||
| $result = $!key.perl ~ ' => ' ~ $!value.perl; | ||
| } | ||
| $result = "(my \\Pair_{self.WHERE} = $result)" if %*perlseen{self.WHICH}:delete == 2; | ||
| $result; | ||
| } | ||
|
|
||
| method fmt($format = "%s\t%s") { | ||
| sprintf($format, $!key, $!value); | ||
| } | ||
|
|
||
| multi method AT-KEY(Pair:D: $key) { $key eq $!key ?? $!value !! Mu } | ||
| multi method EXISTS-KEY(Pair:D: $key) { $key eq $!key } | ||
|
|
||
| method FLATTENABLE_LIST() { nqp::list() } | ||
| method FLATTENABLE_HASH() { nqp::hash($!key, $!value) } | ||
| } | ||
|
|
||
| sub infix:«=>»($key, Mu $value) { | ||
| Pair.new(:$key, :$value) | ||
| multi sub infix:<eqv>(Pair:D $a, Pair:D $b) { | ||
| $a.WHAT === $b.WHAT && $a.key eqv $b.key && $a.value eqv $b.value | ||
| } | ||
|
|
||
| sub pair($key,$value) { Pair.new(:$key,:$value) } | ||
| multi sub infix:<cmp>(Pair:D \a, Pair:D \b) { | ||
| (a.key cmp b.key) || (a.value cmp b.value) | ||
| } | ||
|
|
||
| sub infix:«=>»($key, Mu \value) { | ||
| Pair.new($key, value) | ||
| } | ||
|
|
||
| sub pair($key, \value) { | ||
| Pair.new($key, value) | ||
| } | ||
|
|
||
| # vim: ft=perl6 expandtab sw=4 |
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
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
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
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