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
Initial implementation of Mixy / Mix / MixHash, as per spec (almost)
- Loading branch information
Showing
8 changed files
with
126 additions
and
1 deletion.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| my class Mix does Mixy { | ||
| has $!WHICH; | ||
|
|
||
| method at_key($k --> Real) { | ||
| my $elems := nqp::getattr(self, Mix, '%!elems'); | ||
| my $key := $k.WHICH; | ||
| $elems.exists_key($key) | ||
| ?? $elems{$key}.value | ||
| !! 0; | ||
| } | ||
|
|
||
| method delete ($a --> Real) { # is DEPRECATED doesn't work in settings | ||
| once DEPRECATED("Method 'Mix.delete'","the :delete adverb"); | ||
| self.delete_key($a); | ||
| } | ||
| method delete_key($a --> Real) is hidden_from_backtrace { | ||
| X::Immutable.new( method => 'delete', typename => self.^name ).throw; | ||
| } | ||
|
|
||
| method Mix { self } | ||
| method MixHash { MixHash.new-fp(nqp::getattr(self, Mix, '%!elems').values) } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| my class MixHash does Mixy { | ||
|
|
||
| method at_key($k) { | ||
| Proxy.new( | ||
| FETCH => { | ||
| my $key := $k.WHICH; | ||
| my $elems := nqp::getattr(self, MixHash, '%!elems'); | ||
| $elems.exists_key($key) ?? $elems{$key}.value !! 0; | ||
| }, | ||
| STORE => -> $, $value { | ||
| if $value != 0 { | ||
| (nqp::getattr(self, MixHash, '%!elems'){$k.WHICH} | ||
| //= ($k => 0)).value = $value; | ||
| } | ||
| else { | ||
| self.delete_key($k); | ||
| } | ||
| $value; | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| method delete($k) { # is DEPRECATED doesn't work in settings | ||
| once DEPRECATED("Method 'MixHash.delete'","the :delete adverb"); | ||
| self.delete_key($k); | ||
| } | ||
| method delete_key($k) { | ||
| my $key := $k.WHICH; | ||
| my $elems := nqp::getattr(self, MixHash, '%!elems'); | ||
| if $elems.exists_key($key) { | ||
| my $value = $elems{$key}.value; | ||
| $elems.delete_key($key); | ||
| $value; | ||
| } | ||
| else { | ||
| 0; | ||
| } | ||
| } | ||
|
|
||
| method Mix (:$view) { | ||
| if $view { | ||
| my $mix := nqp::create(Mix); | ||
| $mix.BUILD( :elems(nqp::getattr(self, MixHash, '%!elems')) ); | ||
| $mix; | ||
| } | ||
| else { | ||
| Mix.new-fp(nqp::getattr(self, MixHash, '%!elems').values); | ||
| } | ||
| } | ||
| method MixHash { self } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| my role Mixy does Baggy { # should really be QuantHash, but that's for later | ||
|
|
||
| method default(--> Real) { 0 } | ||
| method total(--> Real) { [+] self.values } | ||
|
|
||
| multi method gist(Mixy:D $ : --> Str) { | ||
| my $name := self.^name; | ||
| ( $name eq 'Mix' ?? 'mix' !! "$name.new" ) | ||
| ~ '(' | ||
| # ~ %!elems.values.map( { "{.key.gist}({.value})" } ).join(', ') | ||
| ~ self.pairs.map( { | ||
| .value == 1 ?? .key.gist !! "{.key.gist}({.value})" | ||
| } ).join(', ') | ||
| ~ ')'; | ||
| } | ||
|
|
||
| method pick ($count = 1) { | ||
| fail ".pick is not supported on a {.self.^name}"; | ||
| } | ||
|
|
||
| method roll ($count = 1) { | ||
| my $total = self.total; | ||
| my $rolls = $count ~~ Num ?? $total min $count !! $count; | ||
| # my @pairs := %!elems.values; | ||
| my @pairs := self.pairs; | ||
|
|
||
| map { | ||
| my $rand = $total.rand; | ||
| my $seen = 0; | ||
| my $roll; | ||
| for @pairs -> $pair { | ||
| next if ( $seen += $pair.value ) <= $rand; | ||
|
|
||
| $roll = $pair.key; | ||
| last; | ||
| } | ||
| $roll; | ||
| }, 1 .. $rolls; | ||
| } | ||
| } |
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