@@ -150,34 +150,49 @@ For associative slices, the angle-brackets form often comes in handy:
150
150
my %color = kiwi => "green", banana => "yellow", cherry => "red";
151
151
dd %color{"cherry", "kiwi"}; #-> ("red", "green")
152
152
dd %color<cherry kiwi>; #-> ("red", "green")
153
+ dd %color{*}; #-> ("green", "red", "yellow")
153
154
154
- Be aware that whether a slice is returned, depends on the I < type > of what is
155
- passed to the subscript, not its length. As long as the outer object passed to
156
- (L < one dimension of|#Multiple dimensions > ) the subscript is a L < Positional > , it
157
- causes a slice to be returned:
155
+ Be aware that slices are controlled by the I < type > of what is passed to
156
+ (L < one dimension of|#Multiple dimensions > ) the subscript, not its length:
158
157
159
- # an Int, which is non-Positional:
160
- dd @alphabet[2]; #-> "c"
158
+ = begin table
159
+ subscript result
160
+ ------------------------------- -----------------------------------------
161
+ any Positional object not normal slice
162
+ covered below
161
163
162
- # a Parcel, which is Positional:
163
- dd @alphabet[2,]; #-> ("c",)
164
- dd @alphabet[()]; #-> ()
164
+ a Range or infinite sequence truncating slice (only for positional
165
+ subscripts)
166
+
167
+ * (Whatever-star) full slice (as if all keys/indices were
168
+ specified)
169
+
170
+ any other object single-element access rather than a slice
171
+
172
+ empty Zen slice
173
+ = end table
174
+
175
+ = comment TODO: Revisit the above table after the GLR, when slicing will
176
+ probably be based on Iterable rather than Positional, and
177
+ truncating based on "known infiniteness".
165
178
166
- = comment TODO: Revisit the above paragraph and code listing after the GLR, when
167
- slicing will probably be based on Iterable rather than
168
- Positional.
179
+ So even a one-element list returns a slice, whereas a bare scalar value doesn't:
180
+
181
+ dd @alphabet[2,]; #-> ("c",)
182
+ dd @alphabet[2]; #-> "c"
169
183
170
- (The angle-brackets form for associative subscripts works out because
184
+ (The angle-bracket form for associative subscripts works out because
171
185
L < word quoting|/language/quoting#Word_quoting:_qw > conveniently returns a
172
186
L < Str > in case of a single word, but a L < Parcel > in case of multiple words.)
173
187
174
188
= comment TODO: Revisit the above paragraph after the GLR, when it will probably
175
189
be a List instead of a Parcel.
176
190
177
- The content of (L < each dimension of|#Multiple dimensions > ) a slice subscript is
178
- I < flattened > before its members are interpreted as indices/keys:
191
+ For a normal slice, the content of (L < the current dimension of|#Multiple
192
+ dimensions > ) the subscript is I < flattened > before its members are interpreted
193
+ as indices/keys:
179
194
180
- dd @alphabet[0, (1..2, 3 ))]; #-> ("a", "b", "c", "d")
195
+ dd @alphabet[0, (1..2, (3,) ))]; #-> ("a", "b", "c", "d")
181
196
182
197
= head2 Truncating slices
183
198
@@ -206,6 +221,31 @@ infinite ranges and sequences:
206
221
If you I < don't > want to specify your slice as a range/sequence but still want
207
222
to silently skip nonexistent elements, you can use the L < #:v > adverb.
208
223
224
+ = head2 Zen slices
225
+
226
+ If you write a subscript without specifying any indices/keys at all, it simply
227
+ returns the subscripted object itself. Since it is empty but returns
228
+ everything, it is known as a "Zen slice".
229
+
230
+ It is different both from passing a Whatever-star (which, like a normal slice,
231
+ always returns a Parcel of elements no matter the type of the original object)
232
+ and from passing an empty list (which returns an empty slice):
233
+
234
+ = comment TODO: Revisit the above paragraph after the GLR, when it will probably
235
+ be a List instead of a Parcel.
236
+
237
+ my %bag := ("orange" => 1, "apple" => 3).Bag;
238
+ dd %bag<>; #-> ("orange"=>1,"apple"=>3).Bag
239
+ dd %bag{}; #-> ("orange"=>1,"apple"=>3).Bag
240
+ dd %bag{*}; #-> (1, 3)
241
+ dd %bag{()}; #-> ()
242
+
243
+ It is usually used to L < interpolate|/language/quoting#Interpolation:_qq >
244
+ entire arrays / hashes into strings:
245
+
246
+ my @words = "cruel", "world";
247
+ say "Hello, @words[]!" #-> Hello, cruel world!
248
+
209
249
= head1 Multiple dimensions
210
250
211
251
...
0 commit comments