/
Mix.pm6
105 lines (96 loc) · 3.2 KB
/
Mix.pm6
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
my class Mix does Mixy {
has ValueObjAt $!WHICH;
has Real $!total;
has Real $!total-positive;
method ^parameterize(Mu \base, Mu \type) {
Rakudo::Internals.PARAMETERIZE-KEYOF(base,type)
}
#--- interface methods
multi method STORE(Mix:D: *@pairs, :$INITIALIZE! --> Mix:D) {
(my \iterator := @pairs.iterator).is-lazy
?? Failure.new(
X::Cannot::Lazy.new(:action<initialize>,:what(self.^name)))
!! self.SET-SELF(Rakudo::QuantHash.ADD-PAIRS-TO-MIX(
nqp::create(Rakudo::Internals::IterationSet),iterator,self.keyof
))
}
multi method STORE(Mix:D: \objects, \values, :$INITIALIZE! --> Mix:D) {
self.SET-SELF(
Rakudo::QuantHash.ADD-OBJECTS-VALUES-TO-MIX(
nqp::create(Rakudo::Internals::IterationSet),
objects.iterator,
values.iterator,
self.keyof
)
)
}
multi method DELETE-KEY(Mix:D: \k) {
X::Immutable.new(method => 'DELETE-KEY', typename => self.^name).throw;
}
#--- introspection methods
multi method WHICH(Mix:D: --> ValueObjAt:D) {
nqp::if(
nqp::attrinited(self,Mix,'$!WHICH'),
$!WHICH,
$!WHICH := nqp::box_s(
nqp::concat(
nqp::if(
nqp::eqaddr(self.WHAT,Mix),
'Mix|',
nqp::concat(nqp::unbox_s(self.^name), '|')
),
nqp::sha1(
nqp::join('\0',Rakudo::Sorting.MERGESORT-str(
Rakudo::QuantHash.BAGGY-RAW-KEY-VALUES(self)
))
)
),
ValueObjAt
)
)
}
method total(Mix:D: --> Real:D) {
nqp::if(
nqp::attrinited(self,Mix,'$!total'),
$!total,
$!total := Rakudo::QuantHash.MIX-TOTAL($!elems)
)
}
method !total-positive(Mix:D: --> Real:D) {
nqp::if(
nqp::attrinited(self,Mix,'$!total-positive'),
$!total-positive,
$!total-positive := Rakudo::QuantHash.MIX-TOTAL-POSITIVE($!elems)
)
}
#--- selection methods
multi method grab($count? --> Real:D) {
X::Immutable.new( method => 'grab', typename => self.^name ).throw;
}
multi method grabpairs($count? --> Real:D) {
X::Immutable.new( method => 'grabpairs', typename => self.^name ).throw;
}
#--- coercion methods
multi method Mix(Mix:D:) { self }
multi method MixHash(Mix:D:) {
nqp::if(
$!elems && nqp::elems($!elems),
nqp::create(MixHash).SET-SELF(Rakudo::QuantHash.BAGGY-CLONE($!elems)),
nqp::create(MixHash)
)
}
multi method Setty(Mix:U:) { Set }
multi method Setty(Mix:D:) { self.Set }
multi method Baggy(Mix:U:) { Bag }
multi method Baggy(Mix:D:) { self.Bag }
multi method Mixy (Mix:U:) { Mix }
multi method Mixy (Mix:D:) { self }
#--- illegal methods
proto method classify-list(|) {
X::Immutable.new(:method<classify-list>, :typename(self.^name)).throw;
}
proto method categorize-list(|) {
X::Immutable.new(:method<categorize-list>, :typename(self.^name)).throw;
}
}
# vim: ft=perl6 expandtab sw=4