Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Introduce direct .min/.max on Bags/BagHashes
Actually inspired by a code example of TheDamian that looked hacky, since one
of the more common uses of Bags/BagHashes are to find out the frequence of
things, and hence you probably want to know the lowest/highest frequency quite
often as well.
  • Loading branch information
lizmat committed Apr 13, 2014
1 parent 80171d8 commit f9c9487
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/core/Bag.pm
@@ -1,10 +1,13 @@
my class Bag does Baggy {
has Int $!total;
has Int $!min;
has Int $!max;
has $!WHICH;

method total {
$!total //= [+] %!elems.values.map( { .value } );
}
method total (--> Int) { $!total //= [+] self.values }
method min (--> Int) { $!min //= self.values.min }
method max (--> Int) { $!max //= self.values.max }

submethod WHICH { $!WHICH }
submethod BUILD (:%elems) {
my @keys := %elems.keys.sort;
Expand Down
2 changes: 2 additions & 0 deletions src/core/Baggy.pm
Expand Up @@ -8,6 +8,8 @@ my role Baggy does QuantHash {
method kv { %!elems.values.map( {.key, .value} ) }
method elems(--> Int) { %!elems.elems }
method total(--> Int) { [+] self.values }
method min(--> Int) { self.values.min }
method max(--> Int) { self.values.max }
method exists ($k --> Bool) { # is DEPRECATED doesn't work in settings
DEPRECATED("the :exists adverb");
self.exists_key($k);
Expand Down

0 comments on commit f9c9487

Please sign in to comment.