@@ -12,7 +12,8 @@ an elegant system for handling them.
12
12
13
13
= head1 Literal Lists
14
14
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:
16
17
17
18
1,2 # This is two-element list
18
19
(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.
45
46
my @a := 1,2,3;
46
47
47
48
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:
50
51
51
52
my @a := 1; # Type check failed in binding; expected Positional but got Int
52
53
@@ -72,12 +73,32 @@ it knows a sequence is infinite, but it cannot always know.
72
73
73
74
= comment TODO link or describe C < ... >
74
75
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 > .
79
79
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
81
102
82
103
= head2 Slips
83
104
@@ -101,7 +122,7 @@ or re-bind existing elements:
101
122
(1,2,3)[0] := 0; # Error Cannot use bind operator with this left-hand side
102
123
(1,2,3)[0] = 0; # Error Cannot modify an immutable Int
103
124
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
105
126
can still change the value which that C < Scalar > points to:
106
127
107
128
my $a = 2;
@@ -135,7 +156,6 @@ you may flatten the list to produce a sequence of values as if all parentheses.
135
156
This works no matter how many levels deep the parenthesis are nested.
136
157
137
158
say (1,(2,(3,4)),5).flat eqv (1,2,3,4,5) # says True
138
- for (1,(2,(3,4)),5).flat { .say } #
139
159
140
160
This is not really a syntactical "context" as much as it is a process of
141
161
iteration, but it has the appearence of a context.
@@ -155,7 +175,7 @@ flattening:
155
175
= head2 Argument List (Capture) Context
156
176
157
177
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
159
179
C < Capture > . A C < Capture > itself has a List (C < .list > ) and a Hash (C < .hash > ).
160
180
Any C < Pair > literals whose keys are not quoted, or which are not parenthesized,
161
181
never make it into C < .list > . Instead, they are considered to be named
@@ -164,16 +184,16 @@ for the details of this processing.
164
184
165
185
Consider the following ways to make a new C < Array > from a C < List > . These ways
166
186
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.
168
188
169
- Array.new(1, 2, :c(3);
189
+ Array.new(1, 2, :c(3)) ;
170
190
Array.new: 1, 2, :c(3);
171
191
new Array: 1, 2, :c(3);
172
192
173
193
In contrast, these ways do not place the C < List > in argument list context,
174
194
so all the elements, even the C < Pair > C < :c(3) > , are placed in the C < Array > .
175
195
176
- Array.new((1, 2, :c(3));
196
+ Array.new((1, 2, :c(3))) ;
177
197
(1, 2, :c(3)).Array;
178
198
my @a = 1, 2, :c(3); Array.new(@a);
179
199
my @a = 1, 2, :c(3); Array.new: @a;
@@ -195,11 +215,28 @@ as named parameters:
195
215
196
216
= comment TODO actualy not sure if there is anything special here, other then the internal itemization best left for later -- eager maybe?
197
217
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.
199
233
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:
201
238
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
203
240
204
241
= head1 Arrays
205
242
0 commit comments