@@ -18,7 +18,7 @@ Arrays to have every value of the list stored in a container.
18
18
In Perl 6, assigning a C < List > to a scalar variable does not lose
19
19
information. The difference is that iteration generally treats a
20
20
list (or any other list-like object, like a L < Seq > or an L < Array > )
21
- inside a scalar as a single element.
21
+ inside a scalar as a single element, as long as it's part of another .
22
22
23
23
my @a = 1, 2, 3;
24
24
for @a { } # three iterations
@@ -28,16 +28,40 @@ inside a scalar as a single element.
28
28
for @a.item { } # one iteration
29
29
for $s.list { } # three iterations
30
30
31
- Lists generally interpolate (flatten) unless they are accessed via
32
- an item (scalar) container.
31
+ Lists generally don't interpolate (flatten) into other lists, except
32
+ when they are not itemized, and the single argument to an operation
33
+ such as C < push > :
33
34
34
35
my @a = 1, 2, 3;
35
- my @flat = @a, @a; # six elements
36
- my @nested = @a.item, @a.item; # two elements
36
+ my @nested = @a, @a; # two elements
37
+ my @flat = flat @a, @a; # six elements, with explict flat
38
+ my @b = 'a', 'b';
39
+ @b.push: @a; # @b now has 5 elements, because @a
40
+ # is the sole argument to push
41
+ my @c = 'a', 'b';
42
+ @c.push: @a, ; # @b now has 3 elements, because of the
43
+ # trailing comma
44
+ my @c = 'a', 'b';
45
+ @c.push: $@a; # @b now has 3 arguments, because @a was
46
+ # itemized
37
47
38
48
C < .item > can often be written as C < $( ... ) > , and on an array variable
39
49
even as C < $@a > .
40
50
51
+ The same flattening behavior applies all objects that do the
52
+ L < Iterable|/type/Iterable > role, notable L < hashes|/type/Hash > :
53
+
54
+ my %h = a => 1, b => 2;
55
+ my @b = %h; say @b.elems; # 2
56
+ my @c = %h, ; say @c.elems; # 1
57
+ my @d = $%h; say @d.elems; # 1
58
+
59
+ Slurpy parameters (C < *@a > ) flattens non-itemized sublists:
60
+
61
+ sub fe(*@flat) { @flat.elems }
62
+ say fe(<a b>, <d e>); # 4
63
+ say fe(<a b>, <d e>.item); # 3
64
+
41
65
= head1 Methods
42
66
43
67
= head2 routine elems
@@ -56,40 +80,40 @@ Returns the index of the last element.
56
80
57
81
= head2 routine keys
58
82
59
- multi sub keys($list) returns List :D
60
- multi method keys(List:D:) returns List :D
83
+ multi sub keys($list) returns Seq :D
84
+ multi method keys(List:D:) returns Seq :D
61
85
62
- Returns a list of indexes into the list (e.g., 0..(@list.elems-1)).
86
+ Returns a sequence of indexes into the list (e.g., 0..(@list.elems-1)).
63
87
64
88
= head2 routine values
65
89
66
- multi sub values($list) returns List :D
67
- multi method values(List:D:) returns List :D
90
+ multi sub values($list) returns Seq :D
91
+ multi method values(List:D:) returns Seq :D
68
92
69
- Returns a copy of the list.
93
+ Returns a sequence of the list elements, in order .
70
94
71
95
= head2 routine kv
72
96
73
- multi sub kv($list) returns List :D
74
- multi method kv(List:D:) returns List :D
97
+ multi sub kv($list) returns Seq :D
98
+ multi method kv(List:D:) returns Seq :D
75
99
76
- Returns an interleaved list of indexes and values. For example
100
+ Returns an interleaved sequence of indexes and values. For example
77
101
78
102
<a b c>.kv
79
103
80
104
Returns
81
105
82
- 0, 'a', 1, 'b', 2, 'c'
106
+ ( 0, 'a', 1, 'b', 2, 'c').Seq
83
107
84
108
= head2 routine pairs
85
109
86
- multi sub pairs($list) returns List :D
87
- multi method pairs(List:D:) returns List :D
110
+ multi sub pairs($list) returns Seq :D
111
+ multi method pairs(List:D:) returns Seq :D
88
112
89
- Returns a list of pairs, with the indexes as keys and the list values as
113
+ Returns a sequence of pairs, with the indexes as keys and the list values as
90
114
values.
91
115
92
- <a b c>.pairs # 0 => 'a', 1 => 'b', 2 => 'c'
116
+ <a b c>.pairs # ( 0 => 'a', 1 => 'b', 2 => 'c').Seq
93
117
94
118
= head2 routine join
95
119
@@ -109,11 +133,11 @@ Note that the method form does not flatten sublists:
109
133
110
134
= head2 routine map
111
135
112
- multi sub map(&code, *@elems) returns List :D
113
- multi method map(List:D: &code) returns List :D
136
+ multi sub map(&code, *@elems) returns Seq :D
137
+ multi method map(List:D: &code) returns Seq :D
114
138
115
- Invokes C < &code > for each element and gathers the return values in another
116
- list and returns it. This happens lazily, i.e. C < &code > is only invoked when
139
+ Invokes C < &code > for each element and gathers the return values in
140
+ a sequence and returns it. This happens lazily, i.e. C < &code > is only invoked when
117
141
the return values are accessed.
118
142
119
143
Examples:
@@ -136,12 +160,12 @@ Note that C<map> does not flatten embedded lists and arrays, so
136
160
((1, 2), <a b>).map({ .join(',')})
137
161
138
162
passes C < (1, 2) > and C << <a b> >> in turn to the block, leading to a total
139
- of two iterations and the result list C < "1,2", "a,b" > .
163
+ of two iterations and the result sequence C < "1,2", "a,b" > .
140
164
See L < method flatmap|#method flatmap > for an alternative that flattens.
141
165
142
166
= head2 method flatmap
143
167
144
- method flatmap(List:D: &code) returns List :D
168
+ method flatmap(List:D: &code) returns Seq :D
145
169
146
170
Like C < map > iterates over the elements of the invocant list, feeding each
147
171
element in turn to the code reference, and assembling the return values from
@@ -155,10 +179,10 @@ invokes C<uc|/type/Str#routine uc> four times.
155
179
156
180
= head2 routine grep
157
181
158
- multi sub grep(Mu $matcher, *@elems) returns List :D
159
- multi method grep(List:D: Mu $matcher) returns List :D
182
+ multi sub grep(Mu $matcher, *@elems) returns Seq :D
183
+ multi method grep(List:D: Mu $matcher) returns Seq :D
160
184
161
- Returns a lazy list of elements against which C < $matcher > smart-matches.
185
+ Returns a sequence of elements against which C < $matcher > smart-matches.
162
186
The elements are returned in the order in which they appear in the original
163
187
list.
164
188
@@ -171,9 +195,9 @@ Examples:
171
195
172
196
= head2 routine grep-index
173
197
174
- multi method grep-index(List:D: Mu $matcher) returns List :D
198
+ multi method grep-index(List:D: Mu $matcher) returns Seq :D
175
199
176
- Returns a lazy list of indices against which the associated elements
200
+ Returns a sequence of indices against which the associated elements
177
201
smart-match. The indices are returned in order.
178
202
179
203
= head2 routine first
@@ -251,8 +275,8 @@ Returns the number of elements in the list (same as C<.elems>).
251
275
252
276
= head2 routine pick
253
277
254
- multi sub pick($count, *@list) returns List :D
255
- multi method pick(List:D: $count = 1)
278
+ multi sub pick($count, *@list) returns Seq :D
279
+ multi method pick(List:D: $count = 1) returns Mu
256
280
257
281
Returns C < $count > elements chosen at random and without repetition
258
282
from the invocant. If C < * > is passed as C < $count > , or C < $count > is
@@ -263,20 +287,20 @@ Examples:
263
287
264
288
say <a b c d e>.pick; # b
265
289
b
266
- say <a b c d e>.pick: 3; # c a e
267
- say <a b c d e>.pick: *; # e d a b c
290
+ say <a b c d e>.pick: 3; # ( c a e)
291
+ say <a b c d e>.pick: *; # ( e d a b c)
268
292
269
293
= head2 routine roll
270
294
271
- multi sub roll($count, *@list) returns List :D
295
+ multi sub roll($count, *@list) returns Seq :D
272
296
multi method roll(List:D: $count = 1)
273
297
274
- Returns a lazy list of C < $count > elements, each randomly selected from the
298
+ Returns a sequence of C < $count > elements, each randomly selected from the
275
299
list. Each random choice is made independently, like a separate die roll
276
300
where each die face is a list element.
277
301
278
- If C < * > is passed to C < $count > , returns a lazy, infinite list of randomly chosen
279
- elements from the original list.
302
+ If C < * > is passed to C < $count > , returns a lazy, infinite sequence of randomly
303
+ chosen elements from the original list.
280
304
281
305
Examples:
282
306
@@ -294,8 +318,6 @@ Examples:
294
318
sub eager(*@elems) returns List:D
295
319
296
320
Evaluates all elements in the list eagerly, and returns them as a list.
297
- If a List signals that it is "known infinite", eager evaluation may
298
- stop at the point where the infinity is detected.
299
321
300
322
= head2 routine reverse
301
323
@@ -326,10 +348,10 @@ Examples:
326
348
327
349
= head2 routine sort
328
350
329
- multi sub sort(*@elems) returns List :D
330
- multi sub sort(&by, *@elems) returns List :D
331
- multi method sort(List:D:) returns List :D
332
- multi method sort(List:D:, &by) returns List :D
351
+ multi sub sort(*@elems) returns Seq :D
352
+ multi sub sort(&by, *@elems) returns Seq :D
353
+ multi method sort(List:D:) returns Seq :D
354
+ multi method sort(List:D:, &by) returns Seq :D
333
355
334
356
Sorts the list, smallest element first. By default C << infix:<cmp> >>
335
357
is used for comparing list elements.
@@ -350,10 +372,10 @@ Examples:
350
372
351
373
= head2 routine unique
352
374
353
- multi sub unique(*@values, :&as) returns List :D
354
- multi method unique(List:D:, :&as) returns List :D
375
+ multi sub unique(*@values, :&as) returns Seq :D
376
+ multi method unique(List:D:, :&as) returns Seq :D
355
377
356
- Returns a list of unique values from the invocant/argument list, such
378
+ Returns a sequence of unique values from the invocant/argument list, such
357
379
that only the first occurrence of each duplicated value remains in the
358
380
result list. C < unique > uses the semantics of the L < === > operator to decide whether
359
381
two objects are the same. The order of the original list is preserved even as
@@ -377,10 +399,10 @@ Example:
377
399
378
400
= head2 routine squish
379
401
380
- multi sub squish(*@values, :&as) returns List :D
381
- multi method squish(List:D:, :&as) returns List :D
402
+ multi sub squish(*@values, :&as) returns Seq :D
403
+ multi method squish(List:D:, :&as) returns Seq :D
382
404
383
- Returns a list of values from the invocant/argument list where runs
405
+ Returns a sequence of values from the invocant/argument list where runs
384
406
of more than one value are replaced with only the first instance.
385
407
Like L < C < unique > > , C < squish > uses the semantics of the L < === > operator to decide
386
408
whether two objects are the same. Unlike L < C < unique > > , this function only
@@ -429,9 +451,9 @@ Example:
429
451
430
452
= head2 routine combinations
431
453
432
- multi method combinations (List:D: Int:D $of) returns List :D
433
- multi method combinations (List:D: Range:D $of = 0..*) returns List :D
434
- multi sub combinations ($n, $k) returns List :D
454
+ multi method combinations (List:D: Int:D $of) returns Seq :D
455
+ multi method combinations (List:D: Range:D $of = 0..*) returns Seq :D
456
+ multi sub combinations ($n, $k) returns Seq :D
435
457
436
458
The C < Int > variant returns all C < $of > -combinations of the invocant list.
437
459
For example
@@ -477,10 +499,10 @@ prints
477
499
478
500
= head2 routine permutations
479
501
480
- multi method permutations(List:D:) returns List :D
481
- multi sub permutations($n) returns List :D
502
+ multi method permutations(List:D:) returns Seq :D
503
+ multi sub permutations($n) returns Seq :D
482
504
483
- Returns all possible permutations of a list as a list of arrays . So
505
+ Returns all possible permutations of a list as a sequence of lists . So
484
506
485
507
say .join('|') for <a b c>.permutations
486
508
@@ -513,9 +535,9 @@ prints
513
535
514
536
= head2 method rotor
515
537
516
- method rotor(*@cycle, Bool() :$partial)
538
+ method rotor(*@cycle, Bool() :$partial) returns Seq:D
517
539
518
- Returns a list of lists, where each sublist is made up of elements of the
540
+ Returns a sequence of lists, where each sublist is made up of elements of the
519
541
invocant.
520
542
521
543
In the simplest case, C < @cycle > contains just one integer, in which case the
@@ -544,59 +566,33 @@ Combining multiple cycles and C<:partial> also works:
544
566
say ('a'..'h').rotor(1 => 1, 3 => -1, :partial).join('|');
545
567
# a|c d e|e|g h
546
568
547
- Note that assigning the list of lists returned from C < rotor > to a variable
548
- will flatten to an C < Array > :
549
-
550
- my @maybe_lol = ('a'..'h').rotor(2 => 1);
551
- @maybe_lol.perl.say; #-> ["a", "b", "d", "e", "g", "h"]<>
552
-
553
- Which probably isn't what one wanted, since the C < rotor > -ed output looks
554
- like this:
555
-
556
- say ('a'..'h').rotor(2 => 1).perl; #-> (("a", "b"), ("d", "e"), ("g", "h"))
557
-
558
- To force a C < List > of C < List > s to be returned, I < bind > the output instead of
559
- assigning it:
560
-
561
- my @really_lol := ('a'..'h').rotor(2 => 1);
562
- @really_lol.perl.say; #-> (("a", "b"), ("d", "e"), ("g", "h"))
563
-
564
569
= head2 routine zip
565
570
566
- sub zip(List:D:, List:D:, ... ) returns Seq:D
571
+ sub zip(**@e ) returns Seq:D
567
572
568
- Zip two or more lists together by interleaving their elements. If the lists
569
- have different lengths, then the lists are zipped to the length of the
570
- shortest list; elements of the longer list(s) are discarded .
573
+ Zips two or more lists or other L < iterables|/type/Iterable > together by
574
+ returning a sequence made of a list of all first elements of all lists, then a
575
+ list of all second elemnts of a list etc .
571
576
572
- In order to support parallel iteration over multiple arrays, Perl 6
573
- has a C < zip > function that builds a list of C < List > objects from the
574
- elements of two or more arrays. In ordinary list context this behaves
575
- as a list of C < Captures > and automatically flattens.
577
+ say .join for zip <a b c>, <d e f>;
576
578
577
- = comment TODO: GLR
579
+ Produces the output
578
580
579
- for zip(@names; @codes) -> $name, $zip {
580
- say "Name: $name; Zip code: $zip";
581
- }
581
+ ad
582
+ be
583
+ cf
582
584
583
585
C < zip > has an infix synonym, the C < Z > operator.
584
586
585
- In an explicitly multidimensional list context, however, the sequences
586
- turn into subarrays, and each element would then have to be unpacked
587
- by the signature:
587
+ say .join for <a b c> Z <d e f>; # same output as above
588
588
589
- for lol(zip(@names; @codes)) -> [$name, $zip] {
590
- say "Name: $name; Zip code: $zip";
591
- }
589
+ When the first input list is exhausted, no more elements are returned; so
590
+ trailing elements from longer input lists are discarded.
592
591
593
- By default the C < zip > function reads to the end of the shortest list, but a
594
- short list may always be extended arbitrarily by putting C < * > after the
595
- final value, which replicates the final value as many times as necessary.
596
- If instead of supplying a default value for short lists, you just wish to
597
- skip missing entries, use L < roundrobin|/type/List#sub_roundrobin > instead:
592
+ If you just wish to skip missing entries in shorter sublists,
593
+ use L < roundrobin|/type/List#sub_roundrobin > instead:
598
594
599
- for roundrobin(@queue1; @queue2; @queue3) -> $next {
595
+ for roundrobin(@queue1, @queue2, @queue3) -> $next {
600
596
...
601
597
}
602
598
@@ -611,7 +607,7 @@ elements but simply skip any undefined value:
611
607
my @a = 1;
612
608
my @b = 1..2;
613
609
my @c = 1..3;
614
- for roundrobin(@a; @b; @c) -> $x { $x.say }
610
+ for flat roundrobin(@a, @b, @c) -> $x { $x.say }
615
611
616
612
will display the following values: C < 1, 1, 1, 2, 2, 3 >
617
613
0 commit comments