-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
Bag.pm
42 lines (38 loc) · 1.33 KB
/
Bag.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
my class Bag does Baggy {
has Int $!total;
has $!WHICH;
method total {
$!total //= [+] %!elems.values.map( { .value } );
}
submethod WHICH { $!WHICH }
submethod BUILD (:%elems) {
my @keys := %elems.keys.sort;
$!WHICH := self.^name
~ '|'
~ @keys.map( { $_ ~ '(' ~ %elems{$_}.value ~ ')' } );
nqp::bindattr(self, Bag, '%!elems', %elems);
}
method at_key($k --> Int) {
my $key := $k.WHICH;
%!elems.exists_key($key)
?? %!elems{$key}.value
!! 0;
}
method delete ($a --> Int) { # is DEPRECATED doesn't work in settings
DEPRECATED("the :delete adverb");
self.delete_key($a);
}
method delete_key($a --> Int) is hidden_from_backtrace {
X::Immutable.new( method => 'delete_key', typename => self.^name ).throw;
}
method grab($count = 1 --> Int) is hidden_from_backtrace {
X::Immutable.new( method => 'grab', typename => self.^name ).throw;
}
method grabpairs($count = 1 --> Int) is hidden_from_backtrace {
X::Immutable.new( method => 'grabpairs', typename => self.^name ).throw;
}
method Bag { self }
method BagHash { BagHash.new-fp(%!elems.values) }
method Mix { Mix.new-fp(%!elems.values) }
method MixHash { MixHash.new-fp(%!elems.values) }
}