-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
MixHash.pm
36 lines (33 loc) · 947 Bytes
/
MixHash.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
my class MixHash does Mixy {
method Mix (:$view) {
if $view {
my $mix := nqp::create(Mix);
$mix.BUILD( :elems(%!elems) );
$mix;
}
else {
Mix.new-from-pairs(%!elems.values);
}
}
method MixHash { self }
method Bag { Bag.new-from-pairs(%!elems.values) }
method BagHash { BagHash.new-from-pairs(%!elems.values) }
multi method AT-KEY(MixHash:D: \k) is rw {
Proxy.new(
FETCH => {
my \v := %!elems.AT-KEY(k.WHICH);
nqp::istype(v,Pair) ?? v.value !! 0;
},
STORE => -> $, $value is copy {
if $value != 0 {
(%!elems.AT-KEY(k.WHICH) //= ((k) => 0)).value = $value;
}
else {
%!elems.DELETE-KEY(k.WHICH);
}
$value;
}
);
}
}
# vim: ft=perl6 expandtab sw=4