5 src/classes/Mapping.pir - Perl 6 hash class and related functions
13 .namespace ['Mapping']
15 .sub 'onload' :anon :load :init
16 .local pmc p6meta, mappingproto
17 p6meta = get_hll_global ['Perl6Object'], '$!P6META'
18 mappingproto = p6meta.'new_class'('Mapping', 'parent'=>'Hash Any')
19 p6meta.'register'('Hash', 'parent'=>mappingproto, 'protoobject'=>mappingproto)
23 .sub 'get_string' :method :vtable
26 iter = new 'Iterator', self
43 Return invocant as a Hash
48 .local pmc result, iter
49 result = new 'Perl6Hash'
50 iter = new 'Iterator', self
52 unless iter goto iter_end
64 Return perl representation of the invocant.
73 unless keys goto iter_end
83 unless keys goto iter_end
94 Returns elements of hash as array of C<Pair(key, value)>
101 iter = new 'Iterator', self
119 iter = new 'Iterator', self
131 .sub 'values' :method
134 iter = new 'Iterator', self
155 =head1 TODO: Functions
165 our List multi method Hash::delete ( *@keys )
166 our Scalar multi method Hash::delete ( $key ) is default
168 Deletes the elements specified by C<$key> or C<$keys> from the invocant.
169 returns the value(s) that were associated to those keys.
173 our Bool multi method Hash::exists ( $key )
175 True if invocant has an element whose key matches C<$key>, false
184 .sub kv :multi(Mapping)
195 multi Int|List Hash::keys ( %hash : MatchTest *@keytests )
196 multi Int|List Hash::kv ( %hash : MatchTest *@keytests )
197 multi Int|(List of Pair) Hash::pairs (%hash : MatchTest *@keytests )
198 multi Int|List Hash::values ( %hash : MatchTest *@keytests )
200 Iterates the elements of C<%hash> in no apparent order, but the order
201 will be the same between successive calls to these functions, as long as
202 C<%hash> doesn't change.
204 If C<@keytests> are provided, only elements whose keys evaluate
205 C<$key ~~ any(@keytests)> as true are iterated.
207 What is returned at each element of the iteration varies with function.
208 C<keys> only returns the key; C<values> the value; C<kv> returns both as
209 a 2 element list in (key, value) order, C<pairs> a C<Pair(key, value)>.
211 Note that C<kv %hash> returns the same as C<zip(keys %hash; values %hash)>
213 In Scalar context, they all return the count of elements that would have
216 The lvalue form of C<keys> is not longer supported. Use the C<.buckets>
227 # vim: expandtab shiftwidth=4 ft=pir: