Skip to content

Commit f86c26d

Browse files
committed
Second pass at list-centric page
1 parent 9da18b2 commit f86c26d

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed

doc/Language/list.pod

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ an elegant system for handling them.
1212
1313
=head1 Literal Lists
1414
15-
Literal lists are created with C<,> B<not> with parentheses, so:
15+
Literal L<C<List>s|/type/List>> are created with commas B<not> with
16+
parentheses, so:
1617
1718
1,2 # This is two-element list
1819
(1,2) # This is also a list, in parenthesis
@@ -45,8 +46,8 @@ C<List> into an C<@>-sigiled variable, you can use binding with C<:=> instead.
4546
my @a := 1,2,3;
4647
4748
One of the ways C<@>-sigiled variables act like lists is by always supporting
48-
<positional subscripting|/language/subscripts>. Anything bound to a C<@>-sigiled
49-
value must support the L</type/positional> role which guarantees this:
49+
L<positional subscripting|/language/subscripts>. Anything bound to a C<@>-sigiled
50+
value must support the L<Positional|/type/Positional> role which guarantees this:
5051
5152
my @a := 1; # Type check failed in binding; expected Positional but got Int
5253
@@ -72,12 +73,32 @@ it knows a sequence is infinite, but it cannot always know.
7273
7374
=comment TODO link or describe C<...>
7475
75-
Just because you are still allowed to subscript a C<Seq> does not mean it
76-
promises to keep values around after you have used them. If you have a very
77-
long sequence, you may want to throw old values away so that memory does
78-
not fill up.
76+
Although the C<Seq> class does provide some positional subscripting, it does
77+
not provide the full interface of C<Positional>, so an C<@>-sigiled variable
78+
may B<not> be bound to a C<Seq>.
7979
80-
=comment TODO document .iterator, .list and .cache
80+
my @s := (loop { 42.say }); # Error expected Positional but got Seq
81+
82+
This is because the C<Seq> does not keep values around after you have used them.
83+
This is useful behavior if you have a very long sequence, as you may want to
84+
throw values away after using them, so that your program does not fill up memory.
85+
86+
On the other hand, you may want to keep old values around in some cases.
87+
It is possible to hide a C<Seq> inside a C<List>, which will still be lazy,
88+
but will remember old values -- this is done by calling the C<.list> method.
89+
Since this C<List> fully supports C<Positional>, you may bind it directly
90+
to an C<@>-sigiled variable.
91+
92+
my @s := (loop { 42.say }).list;
93+
@s[2]; # Says 42 three times
94+
@s[1]; # does not say anything
95+
@s[4]; # Says 42 two more times
96+
97+
You may also use the C<.cache> method instead of C<.list>, depending
98+
on how you want the references handled. See the C<page on C<Seq>|/type/Seq>
99+
for details.
100+
101+
=comment TODO document .iterator
81102
82103
=head2 Slips
83104
@@ -101,7 +122,7 @@ or re-bind existing elements:
101122
(1,2,3)[0] := 0; # Error Cannot use bind operator with this left-hand side
102123
(1,2,3)[0] = 0; # Error Cannot modify an immutable Int
103124
104-
However, if any of the elements is wrapped in a L<C<Scalar>|/types/Scalar> you
125+
However, if any of the elements is wrapped in a L<C<Scalar>|/type/Scalar> you
105126
can still change the value which that C<Scalar> points to:
106127
107128
my $a = 2;
@@ -135,7 +156,6 @@ you may flatten the list to produce a sequence of values as if all parentheses.
135156
This works no matter how many levels deep the parenthesis are nested.
136157
137158
say (1,(2,(3,4)),5).flat eqv (1,2,3,4,5) # says True
138-
for (1,(2,(3,4)),5).flat { .say } #
139159
140160
This is not really a syntactical "context" as much as it is a process of
141161
iteration, but it has the appearence of a context.
@@ -155,7 +175,7 @@ flattening:
155175
=head2 Argument List (Capture) Context
156176
157177
When a list appears as arguments to a function or method call, special
158-
syntax rules are at play: the list immediately is converted into a
178+
syntax rules are at play: the list is immediately converted into a
159179
C<Capture>. A C<Capture> itself has a List (C<.list>) and a Hash (C<.hash>).
160180
Any C<Pair> literals whose keys are not quoted, or which are not parenthesized,
161181
never make it into C<.list>. Instead, they are considered to be named
@@ -164,16 +184,16 @@ for the details of this processing.
164184
165185
Consider the following ways to make a new C<Array> from a C<List>. These ways
166186
place the C<List> in an argument list context and because of that, the C<Array>
167-
only contains C<1> and C<2> but not the C<pair> C<:c3>, which is ignored.
187+
only contains C<1> and C<2> but not the C<Pair> C<:c(3)>, which is ignored.
168188
169-
Array.new(1, 2, :c(3);
189+
Array.new(1, 2, :c(3));
170190
Array.new: 1, 2, :c(3);
171191
new Array: 1, 2, :c(3);
172192
173193
In contrast, these ways do not place the C<List> in argument list context,
174194
so all the elements, even the C<Pair> C<:c(3)>, are placed in the C<Array>.
175195
176-
Array.new((1, 2, :c(3));
196+
Array.new((1, 2, :c(3)));
177197
(1, 2, :c(3)).Array;
178198
my @a = 1, 2, :c(3); Array.new(@a);
179199
my @a = 1, 2, :c(3); Array.new: @a;
@@ -195,11 +215,28 @@ as named parameters:
195215
196216
=comment TODO actualy not sure if there is anything special here, other then the internal itemization best left for later -- eager maybe?
197217
198-
=head2 Slice Context
218+
=head2 Slice Indexing Context
219+
220+
From the perspective of the C<List> inside a L<slice subscript|/language/subscripts#Slices>,
221+
is only remarkable in that it is unremarkable: because
222+
L<adverbs|/language/subscripts#Adverbs> to a slice are attached after the C<]>,
223+
the inside of a slice is B<not> an argument list, and no special processing
224+
of pair forms happens.
225+
226+
Most C<Positional> types will enforce an integer coercion on each element
227+
of a slice index, so pairs appearing there will generate an error, anyway:
228+
229+
(1,2,3)[1, 2, :c(3)] # Method 'Int' not found for invocant of class 'Pair'
230+
231+
...however this is entirely up to the type -- if it defines an order
232+
for pairs, it could consider C<:c(3)> a valid index.
199233
200-
(1,2,3)[1, 2, :c(3)]
234+
Indices inside a slice are usually not automatically flattened, but
235+
neither are sublists usually coerced to C<Int>. Instead, the list structure
236+
is kept intact, causing a nested slice operation that replicates the
237+
structure in the result:
201238
202-
=comment TODO there is a typecheck against key/index, anything else?
239+
say ("a","b","c")[(1,2),(0,1)] eqv (("b", "c"), ("a", "b")) # says True
203240
204241
=head1 Arrays
205242

0 commit comments

Comments
 (0)