Permalink
Browse files

Make Set|Bag|Mix.total caching, as suggested by grondilu++

  • Loading branch information...
lizmat committed Oct 5, 2013
1 parent 1edf477 commit ae27d10ad055e231d9791bda0a231434cc822c97
Showing with 12 additions and 0 deletions.
  1. +5 −0 src/core/Bag.pm
  2. +5 −0 src/core/Mix.pm
  3. +2 −0 src/core/Set.pm
View
@@ -1,6 +1,11 @@
my class Bag does Baggy {
+ has Int $!total;
has $!WHICH;
+ method total {
+ $!total //=
+ [+] nqp::getattr(self, Bag, '%!elems').values.map( { .value } );
+ }
submethod WHICH { $!WHICH }
submethod BUILD (:%elems) {
my @keys := %elems.keys.sort;
View
@@ -1,5 +1,10 @@
my class Mix does Mixy {
+ has Real $!total;
+ method total {
+ $!total //=
+ [+] nqp::getattr(self, Mix, '%!elems').values.map( { .value } );
+ }
method at_key($k --> Real) {
my $elems := nqp::getattr(self, Mix, '%!elems');
my $key := $k.WHICH;
View
@@ -1,6 +1,8 @@
my class Set does Setty {
+ has Int $!total;
has $!WHICH;
+ method total { $!total //= nqp::getattr(self, Set, '%!elems').elems }
submethod WHICH { $!WHICH }
submethod BUILD (:%elems) {
my @keys := %elems.keys.sort;

0 comments on commit ae27d10

Please sign in to comment.