Re: How to accomplish a method interpolation?
Ohhh I see the parens are significant. Now this works :) method !show-item ($msg, $item, $attr) { say " $msg ", %!items{$item}."$attr"() if %!items{$item}:exists; } method show { for { self!show-item(.values.tc, .values, 'amount'); } self!show-item('Vida útil de la antorcha', 'antorcha', 'duration'); } 2017-12-30 5:54 GMT-03:00 Siavash: > > What you want is `%items{$item}."$attr"()`. > > But if all you want is removing the show's repetition, maybe there are > other ways, for example: > > for %!items:kv { > say $^key.tc, ' ', $^value.amount; > } > > or if you want all items: > > for %!items { > say .key.tc, ' ', .value.amount; > } > > On 2017-12-30 05:13:43 GMT, clasclin . wrote: > > I'm reading a book "Make your own python text adventure" and decided to > > give it a try with perl6. So this code works as expected. > > > > class Bag { > > has %.items; > > > > method show { > > say "ropa ", %!items.amount > > if %!items:exists; > > say "femur ", %!items.amount > > if %!items:exists; > > say "fósforos ", %!items .amount > > if %!items :exists; > > } > > } > > > > I'm using a class called bag as my inventory and trying to keep track of > > items in a hash, so the key is just a name and the value is an object and > > just found that a pattern when I try to list the elements on the > screen... > > > > dd %!items > > Hash %!items = {:femur(Items::Femur.new(amount => 1)), > > :fósforos(Items::Matches.new(amount => 3)), > :ropa(Items::Cloth.new(amount > > => 4))} > > > > Items has data and the show method does what I expect, then I change the > > code, so I try to add a private method that handdle the pattern and > change > > the show method to use the private one. > > > > class Bag { > > has %.items; > > > > method !show-item ($msg, $item, $attr) { > > # dd $msg; dd $item; dd $attr; # all Str > > say "$msg ", %items{$item}.$attr > > if %!items{$item}:exists; > > } > > > > method show { > > self!show-item('Ropa', 'ropa', 'amount'); > > ... > > } > > } > > > > Now I get an error, the line corresponds to the private method > > No such method 'CALL-ME' for invocant of type 'Str' > > > > Try a few variants like return instead of say or things like > > say "$msg", "%!items{$item}.$attr"; # output: Ropa > > Items::Cloth<94810994868552>.amount > > say "$msg", $!items{$item}.{$attr}; # output: Type Items::Cloth does > > not support associative indexing. > > say "$msg", %!items{$item}."{$attr}"; # ouput: gives a compile error > > > > The output I'm expecting is: Ropa 4 > > Ropa is the $msg, and 4 corresponds to the value in the attribute amount. > > > > Is there a way to refactor my code? Thanks in advance. > >
Re: How to accomplish a method interpolation?
Uh, it's a D thing. It's not just a programmatic container representing inventory, but has its own gameplay qualities (in original D, for example, it reduced inventory weight by a lot). I'd guess that currently it's not doing anything special, but might well be intended to do so later --- either "magical" like the original bag of holding, or just providing methods for in-game inventory management functions, or etc. Some of that might be doable by subclassing a mutable container, but others might well require something smarter: for example, some items "stack" (one arrow is like any other arrow, at least if they have the same enchantment) and others must be duplicated (every wand has its own independent charge state and must not be combined even if they have the same charge currently), and rather than making the code using it figure out how the container associated with each key works, you build that into the object. On Sat, Dec 30, 2017 at 7:07 AM, Elizabeth Mattijsenwrote: > But that's the kind of semantic that Bag/BagHashes are meant to perform: a > "bag of holding”! Or am I missing something? > > > > On 30 Dec 2017, at 12:55, Brandon Allbery wrote: > > > > I think this is just name collision; this sounds like a dungeon crawler > type thing and it's a "bag of holding". > > > > On Sat, Dec 30, 2017 at 6:45 AM, Elizabeth Mattijsen > wrote: > > > On 30 Dec 2017, at 06:13, clasclin . wrote: > > > > > > I'm reading a book "Make your own python text adventure" and decided > to give it a try with perl6. So this code works as expected. > > > > > > class Bag { > > > has %.items; > > > > > > method show { > > > say "ropa ", %!items.amount > > > if %!items:exists; > > > say "femur ", %!items.amount > > > if %!items:exists; > > > say "fósforos ", %!items .amount > > > if %!items :exists; > > > } > > > } > > > > > > I'm using a class called bag as my inventory and trying to keep track > of items in a hash, so the key is just a name and the value is an object > and just found that a pattern when I try to list the elements on the > screen... > > > > > > dd %!items > > > Hash %!items = {:femur(Items::Femur.new(amount => 1)), > :fósforos(Items::Matches.new(amount => 3)), :ropa(Items::Cloth.new(amount > => 4))} > > > > > > Items has data and the show method does what I expect, then I change > the code, so I try to add a private method that handdle the pattern and > change the show method to use the private one. > > > > > > class Bag { > > > has %.items; > > > > > > method !show-item ($msg, $item, $attr) { > > > # dd $msg; dd $item; dd $attr; # all Str > > > say "$msg ", %items{$item}.$attr > > > if %!items{$item}:exists; > > > } > > > > > > method show { > > > self!show-item('Ropa', 'ropa', 'amount'); > > > ... > > > } > > > } > > > > > > Now I get an error, the line corresponds to the private method > > > No such method 'CALL-ME' for invocant of type 'Str' > > > > > > Try a few variants like return instead of say or things like > > > say "$msg", "%!items{$item}.$attr"; # output: Ropa > Items::Cloth<94810994868552>.amount > > > say "$msg", $!items{$item}.{$attr}; # output: Type Items::Cloth > does not support associative indexing. > > > say "$msg", %!items{$item}."{$attr}"; # ouput: gives a compile > error > > > > > > The output I'm expecting is: Ropa 4 > > > Ropa is the $msg, and 4 corresponds to the value in the attribute > amount. > > > > > > Is there a way to refactor my code? Thanks in advance. > > > > Are you aware that Perl 6 has an immutable Bag class ( > https://docs.perl6.org/type/Bag ) > > and mutable BagHash class ( https://docs.perl6.org/type/BagHash ) > built in ??? > > > > > > > > -- > > brandon s allbery kf8nh sine nomine > associates > > allber...@gmail.com > ballb...@sinenomine.net > > unix, openafs, kerberos, infrastructure, xmonad > http://sinenomine.net > -- brandon s allbery kf8nh sine nomine associates allber...@gmail.com ballb...@sinenomine.net unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net
Re: How to accomplish a method interpolation?
But that's the kind of semantic that Bag/BagHashes are meant to perform: a "bag of holding”! Or am I missing something? > On 30 Dec 2017, at 12:55, Brandon Allberywrote: > > I think this is just name collision; this sounds like a dungeon crawler type > thing and it's a "bag of holding". > > On Sat, Dec 30, 2017 at 6:45 AM, Elizabeth Mattijsen wrote: > > On 30 Dec 2017, at 06:13, clasclin . wrote: > > > > I'm reading a book "Make your own python text adventure" and decided to > > give it a try with perl6. So this code works as expected. > > > > class Bag { > > has %.items; > > > > method show { > > say "ropa ", %!items.amount > > if %!items:exists; > > say "femur ", %!items.amount > > if %!items:exists; > > say "fósforos ", %!items .amount > > if %!items :exists; > > } > > } > > > > I'm using a class called bag as my inventory and trying to keep track of > > items in a hash, so the key is just a name and the value is an object and > > just found that a pattern when I try to list the elements on the screen... > > > > dd %!items > > Hash %!items = {:femur(Items::Femur.new(amount => 1)), > > :fósforos(Items::Matches.new(amount => 3)), :ropa(Items::Cloth.new(amount > > => 4))} > > > > Items has data and the show method does what I expect, then I change the > > code, so I try to add a private method that handdle the pattern and change > > the show method to use the private one. > > > > class Bag { > > has %.items; > > > > method !show-item ($msg, $item, $attr) { > > # dd $msg; dd $item; dd $attr; # all Str > > say "$msg ", %items{$item}.$attr > > if %!items{$item}:exists; > > } > > > > method show { > > self!show-item('Ropa', 'ropa', 'amount'); > > ... > > } > > } > > > > Now I get an error, the line corresponds to the private method > > No such method 'CALL-ME' for invocant of type 'Str' > > > > Try a few variants like return instead of say or things like > > say "$msg", "%!items{$item}.$attr"; # output: Ropa > > Items::Cloth<94810994868552>.amount > > say "$msg", $!items{$item}.{$attr}; # output: Type Items::Cloth does > > not support associative indexing. > > say "$msg", %!items{$item}."{$attr}"; # ouput: gives a compile error > > > > The output I'm expecting is: Ropa 4 > > Ropa is the $msg, and 4 corresponds to the value in the attribute amount. > > > > Is there a way to refactor my code? Thanks in advance. > > Are you aware that Perl 6 has an immutable Bag class ( > https://docs.perl6.org/type/Bag ) > and mutable BagHash class ( https://docs.perl6.org/type/BagHash ) built in > ??? > > > > -- > brandon s allbery kf8nh sine nomine associates > allber...@gmail.com ballb...@sinenomine.net > unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net
Re: How to accomplish a method interpolation?
I think this is just name collision; this sounds like a dungeon crawler type thing and it's a "bag of holding". On Sat, Dec 30, 2017 at 6:45 AM, Elizabeth Mattijsenwrote: > > On 30 Dec 2017, at 06:13, clasclin . wrote: > > > > I'm reading a book "Make your own python text adventure" and decided to > give it a try with perl6. So this code works as expected. > > > > class Bag { > > has %.items; > > > > method show { > > say "ropa ", %!items.amount > > if %!items:exists; > > say "femur ", %!items.amount > > if %!items:exists; > > say "fósforos ", %!items .amount > > if %!items :exists; > > } > > } > > > > I'm using a class called bag as my inventory and trying to keep track of > items in a hash, so the key is just a name and the value is an object and > just found that a pattern when I try to list the elements on the screen... > > > > dd %!items > > Hash %!items = {:femur(Items::Femur.new(amount => 1)), > :fósforos(Items::Matches.new(amount => 3)), :ropa(Items::Cloth.new(amount > => 4))} > > > > Items has data and the show method does what I expect, then I change the > code, so I try to add a private method that handdle the pattern and change > the show method to use the private one. > > > > class Bag { > > has %.items; > > > > method !show-item ($msg, $item, $attr) { > > # dd $msg; dd $item; dd $attr; # all Str > > say "$msg ", %items{$item}.$attr > > if %!items{$item}:exists; > > } > > > > method show { > > self!show-item('Ropa', 'ropa', 'amount'); > > ... > > } > > } > > > > Now I get an error, the line corresponds to the private method > > No such method 'CALL-ME' for invocant of type 'Str' > > > > Try a few variants like return instead of say or things like > > say "$msg", "%!items{$item}.$attr"; # output: Ropa > Items::Cloth<94810994868552>.amount > > say "$msg", $!items{$item}.{$attr}; # output: Type Items::Cloth does > not support associative indexing. > > say "$msg", %!items{$item}."{$attr}"; # ouput: gives a compile error > > > > The output I'm expecting is: Ropa 4 > > Ropa is the $msg, and 4 corresponds to the value in the attribute amount. > > > > Is there a way to refactor my code? Thanks in advance. > > Are you aware that Perl 6 has an immutable Bag class ( > https://docs.perl6.org/type/Bag ) > and mutable BagHash class ( https://docs.perl6.org/type/BagHash ) built > in ??? -- brandon s allbery kf8nh sine nomine associates allber...@gmail.com ballb...@sinenomine.net unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net
Re: How to accomplish a method interpolation?
> On 30 Dec 2017, at 06:13, clasclin .wrote: > > I'm reading a book "Make your own python text adventure" and decided to give > it a try with perl6. So this code works as expected. > > class Bag { > has %.items; > > method show { > say "ropa ", %!items.amount > if %!items:exists; > say "femur ", %!items.amount > if %!items:exists; > say "fósforos ", %!items .amount > if %!items :exists; > } > } > > I'm using a class called bag as my inventory and trying to keep track of > items in a hash, so the key is just a name and the value is an object and > just found that a pattern when I try to list the elements on the screen... > > dd %!items > Hash %!items = {:femur(Items::Femur.new(amount => 1)), > :fósforos(Items::Matches.new(amount => 3)), :ropa(Items::Cloth.new(amount => > 4))} > > Items has data and the show method does what I expect, then I change the > code, so I try to add a private method that handdle the pattern and change > the show method to use the private one. > > class Bag { > has %.items; > > method !show-item ($msg, $item, $attr) { > # dd $msg; dd $item; dd $attr; # all Str > say "$msg ", %items{$item}.$attr > if %!items{$item}:exists; > } > > method show { > self!show-item('Ropa', 'ropa', 'amount'); > ... > } > } > > Now I get an error, the line corresponds to the private method > No such method 'CALL-ME' for invocant of type 'Str' > > Try a few variants like return instead of say or things like > say "$msg", "%!items{$item}.$attr"; # output: Ropa > Items::Cloth<94810994868552>.amount > say "$msg", $!items{$item}.{$attr}; # output: Type Items::Cloth does not > support associative indexing. > say "$msg", %!items{$item}."{$attr}"; # ouput: gives a compile error > > The output I'm expecting is: Ropa 4 > Ropa is the $msg, and 4 corresponds to the value in the attribute amount. > > Is there a way to refactor my code? Thanks in advance. Are you aware that Perl 6 has an immutable Bag class ( https://docs.perl6.org/type/Bag ) and mutable BagHash class ( https://docs.perl6.org/type/BagHash ) built in ???
Re: How to accomplish a method interpolation?
What you want is `%items{$item}."$attr"()`. But if all you want is removing the show's repetition, maybe there are other ways, for example: for %!items:kv { say $^key.tc, ' ', $^value.amount; } or if you want all items: for %!items { say .key.tc, ' ', .value.amount; } On 2017-12-30 05:13:43 GMT, clasclin . wrote: > I'm reading a book "Make your own python text adventure" and decided to > give it a try with perl6. So this code works as expected. > > class Bag { > has %.items; > > method show { > say "ropa ", %!items.amount > if %!items:exists; > say "femur ", %!items.amount > if %!items:exists; > say "fósforos ", %!items.amount > if %!items :exists; > } > } > > I'm using a class called bag as my inventory and trying to keep track of > items in a hash, so the key is just a name and the value is an object and > just found that a pattern when I try to list the elements on the screen... > > dd %!items > Hash %!items = {:femur(Items::Femur.new(amount => 1)), > :fósforos(Items::Matches.new(amount => 3)), :ropa(Items::Cloth.new(amount > => 4))} > > Items has data and the show method does what I expect, then I change the > code, so I try to add a private method that handdle the pattern and change > the show method to use the private one. > > class Bag { > has %.items; > > method !show-item ($msg, $item, $attr) { > # dd $msg; dd $item; dd $attr; # all Str > say "$msg ", %items{$item}.$attr > if %!items{$item}:exists; > } > > method show { > self!show-item('Ropa', 'ropa', 'amount'); > ... > } > } > > Now I get an error, the line corresponds to the private method > No such method 'CALL-ME' for invocant of type 'Str' > > Try a few variants like return instead of say or things like > say "$msg", "%!items{$item}.$attr"; # output: Ropa > Items::Cloth<94810994868552>.amount > say "$msg", $!items{$item}.{$attr}; # output: Type Items::Cloth does > not support associative indexing. > say "$msg", %!items{$item}."{$attr}"; # ouput: gives a compile error > > The output I'm expecting is: Ropa 4 > Ropa is the $msg, and 4 corresponds to the value in the attribute amount. > > Is there a way to refactor my code? Thanks in advance.