-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
Set.pm
43 lines (37 loc) · 1.29 KB
/
Set.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
43
my class Set does Setty {
has Int $!total;
has $!WHICH;
method total (--> Int) { $!total //= %!elems.elems }
multi method WHICH (Set:D:) {
$!WHICH := self.^name ~ '|' ~ %!elems.keys.sort if !$!WHICH.defined;
$!WHICH
}
submethod BUILD (:%elems) {
nqp::bindattr(self, Set, '%!elems', %elems);
self;
}
method grab ($count?) {
X::Immutable.new( method => 'grab', typename => self.^name ).throw;
}
method grabpairs ($count?) {
X::Immutable.new( method => 'grabpairs', typename => self.^name ).throw;
}
multi method pairs(Set:D:) { # must copy else we can change the Set
%!elems.values.map: { Enum.new(:key($_),:value(True)) };
}
multi method antipairs(Set:D:) { # must copy else we can change the Set
%!elems.values.map: { Enum.new(:key(True),:value($_)) };
}
method Set { self }
method SetHash { SetHash.new(self.keys) }
multi method AT-KEY(Set:D: \k --> Bool) {
%!elems.EXISTS-KEY(k.WHICH);
}
multi method ASSIGN-KEY(Set:D: \k,\v) {
fail X::Assignment::RO.new(typename => self.^name);
}
multi method DELETE-KEY(Set:D: \k) {
fail X::Immutable.new(method => 'DELETE-KEY', typename => self.^name);
}
}
# vim: ft=perl6 expandtab sw=4