Skip to content

Commit 645466d

Browse files
committed
GLRify List.pod
1 parent 4801399 commit 645466d

File tree

1 file changed

+96
-100
lines changed

1 file changed

+96
-100
lines changed

lib/Type/List.pod

Lines changed: 96 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Arrays to have every value of the list stored in a container.
1818
In Perl 6, assigning a C<List> to a scalar variable does not lose
1919
information. The difference is that iteration generally treats a
2020
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 .
2222
2323
my @a = 1, 2, 3;
2424
for @a { } # three iterations
@@ -28,16 +28,40 @@ inside a scalar as a single element.
2828
for @a.item { } # one iteration
2929
for $s.list { } # three iterations
3030
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>:
3334
3435
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
3747
3848
C<.item> can often be written as C<$( ... )>, and on an array variable
3949
even as C<$@a>.
4050
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+
4165
=head1 Methods
4266
4367
=head2 routine elems
@@ -56,40 +80,40 @@ Returns the index of the last element.
5680
5781
=head2 routine keys
5882
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
6185
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)).
6387
6488
=head2 routine values
6589
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
6892
69-
Returns a copy of the list.
93+
Returns a sequence of the list elements, in order.
7094
7195
=head2 routine kv
7296
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
7599
76-
Returns an interleaved list of indexes and values. For example
100+
Returns an interleaved sequence of indexes and values. For example
77101
78102
<a b c>.kv
79103
80104
Returns
81105
82-
0, 'a', 1, 'b', 2, 'c'
106+
(0, 'a', 1, 'b', 2, 'c').Seq
83107
84108
=head2 routine pairs
85109
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
88112
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
90114
values.
91115
92-
<a b c>.pairs # 0 => 'a', 1 => 'b', 2 => 'c'
116+
<a b c>.pairs # (0 => 'a', 1 => 'b', 2 => 'c').Seq
93117
94118
=head2 routine join
95119
@@ -109,11 +133,11 @@ Note that the method form does not flatten sublists:
109133
110134
=head2 routine map
111135
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
114138
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
117141
the return values are accessed.
118142
119143
Examples:
@@ -136,12 +160,12 @@ Note that C<map> does not flatten embedded lists and arrays, so
136160
((1, 2), <a b>).map({ .join(',')})
137161
138162
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">.
140164
See L<method flatmap|#method flatmap> for an alternative that flattens.
141165
142166
=head2 method flatmap
143167
144-
method flatmap(List:D: &code) returns List:D
168+
method flatmap(List:D: &code) returns Seq:D
145169
146170
Like C<map> iterates over the elements of the invocant list, feeding each
147171
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.
155179
156180
=head2 routine grep
157181
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
160184
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.
162186
The elements are returned in the order in which they appear in the original
163187
list.
164188
@@ -171,9 +195,9 @@ Examples:
171195
172196
=head2 routine grep-index
173197
174-
multi method grep-index(List:D: Mu $matcher) returns List:D
198+
multi method grep-index(List:D: Mu $matcher) returns Seq:D
175199
176-
Returns a lazy list of indices against which the associated elements
200+
Returns a sequence of indices against which the associated elements
177201
smart-match. The indices are returned in order.
178202
179203
=head2 routine first
@@ -251,8 +275,8 @@ Returns the number of elements in the list (same as C<.elems>).
251275
252276
=head2 routine pick
253277
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
256280
257281
Returns C<$count> elements chosen at random and without repetition
258282
from the invocant. If C<*> is passed as C<$count>, or C<$count> is
@@ -263,20 +287,20 @@ Examples:
263287
264288
say <a b c d e>.pick; # b
265289
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)
268292
269293
=head2 routine roll
270294
271-
multi sub roll($count, *@list) returns List:D
295+
multi sub roll($count, *@list) returns Seq:D
272296
multi method roll(List:D: $count = 1)
273297
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
275299
list. Each random choice is made independently, like a separate die roll
276300
where each die face is a list element.
277301
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.
280304
281305
Examples:
282306
@@ -294,8 +318,6 @@ Examples:
294318
sub eager(*@elems) returns List:D
295319
296320
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.
299321
300322
=head2 routine reverse
301323
@@ -326,10 +348,10 @@ Examples:
326348
327349
=head2 routine sort
328350
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
333355
334356
Sorts the list, smallest element first. By default C<< infix:<cmp> >>
335357
is used for comparing list elements.
@@ -350,10 +372,10 @@ Examples:
350372
351373
=head2 routine unique
352374
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
355377
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
357379
that only the first occurrence of each duplicated value remains in the
358380
result list. C<unique> uses the semantics of the L<===> operator to decide whether
359381
two objects are the same. The order of the original list is preserved even as
@@ -377,10 +399,10 @@ Example:
377399
378400
=head2 routine squish
379401
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
382404
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
384406
of more than one value are replaced with only the first instance.
385407
Like L<C<unique>>, C<squish> uses the semantics of the L<===> operator to decide
386408
whether two objects are the same. Unlike L<C<unique>>, this function only
@@ -429,9 +451,9 @@ Example:
429451
430452
=head2 routine combinations
431453
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
435457
436458
The C<Int> variant returns all C<$of>-combinations of the invocant list.
437459
For example
@@ -477,10 +499,10 @@ prints
477499
478500
=head2 routine permutations
479501
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
482504
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
484506
485507
say .join('|') for <a b c>.permutations
486508
@@ -513,9 +535,9 @@ prints
513535
514536
=head2 method rotor
515537
516-
method rotor(*@cycle, Bool() :$partial)
538+
method rotor(*@cycle, Bool() :$partial) returns Seq:D
517539
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
519541
invocant.
520542
521543
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:
544566
say ('a'..'h').rotor(1 => 1, 3 => -1, :partial).join('|');
545567
# a|c d e|e|g h
546568
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-
564569
=head2 routine zip
565570
566-
sub zip(List:D:, List:D:, ...) returns Seq:D
571+
sub zip(**@e) returns Seq:D
567572
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.
571576
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>;
576578
577-
=comment TODO: GLR
579+
Produces the output
578580
579-
for zip(@names; @codes) -> $name, $zip {
580-
say "Name: $name; Zip code: $zip";
581-
}
581+
ad
582+
be
583+
cf
582584
583585
C<zip> has an infix synonym, the C<Z> operator.
584586
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
588588
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.
592591
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:
598594
599-
for roundrobin(@queue1; @queue2; @queue3) -> $next {
595+
for roundrobin(@queue1, @queue2, @queue3) -> $next {
600596
...
601597
}
602598
@@ -611,7 +607,7 @@ elements but simply skip any undefined value:
611607
my @a = 1;
612608
my @b = 1..2;
613609
my @c = 1..3;
614-
for roundrobin(@a; @b; @c) -> $x { $x.say }
610+
for flat roundrobin(@a, @b, @c) -> $x { $x.say }
615611
616612
will display the following values: C<1, 1, 1, 2, 2, 3>
617613

0 commit comments

Comments
 (0)