Skip to content

Commit bdd43d9

Browse files
committed
subscripts#Slices: improve clarity; document Zen & Whatever slices
1 parent f497cd4 commit bdd43d9

File tree

1 file changed

+56
-16
lines changed

1 file changed

+56
-16
lines changed

lib/Language/subscripts.pod

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,34 +150,49 @@ For associative slices, the angle-brackets form often comes in handy:
150150
my %color = kiwi => "green", banana => "yellow", cherry => "red";
151151
dd %color{"cherry", "kiwi"}; #-> ("red", "green")
152152
dd %color<cherry kiwi>; #-> ("red", "green")
153+
dd %color{*}; #-> ("green", "red", "yellow")
153154
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:
158157
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
161163
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".
165178
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"
169183
170-
(The angle-brackets form for associative subscripts works out because
184+
(The angle-bracket form for associative subscripts works out because
171185
L<word quoting|/language/quoting#Word_quoting:_qw> conveniently returns a
172186
L<Str> in case of a single word, but a L<Parcel> in case of multiple words.)
173187
174188
=comment TODO: Revisit the above paragraph after the GLR, when it will probably
175189
be a List instead of a Parcel.
176190
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:
179194
180-
dd @alphabet[0, (1..2, 3))]; #-> ("a", "b", "c", "d")
195+
dd @alphabet[0, (1..2, (3,)))]; #-> ("a", "b", "c", "d")
181196
182197
=head2 Truncating slices
183198
@@ -206,6 +221,31 @@ infinite ranges and sequences:
206221
If you I<don't> want to specify your slice as a range/sequence but still want
207222
to silently skip nonexistent elements, you can use the L<#:v> adverb.
208223
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+
209249
=head1 Multiple dimensions
210250
211251
...

0 commit comments

Comments
 (0)