4
4
5
5
= SUBTITLE Positional data constructs
6
6
7
- Lists have been a central part of computing since before there were computers,
8
- during which time many devils have taken up residence in their details. They
9
- were actually one of the hardest parts of Perl 6 to design, but through
10
- persistence and patience, Perl 6 has arrived with an elegant system for handling
11
- them.
7
+ Lists have been a central part of computing since before there were
8
+ computers, during which time many devils have taken up residence in
9
+ their details. They were actually one of the hardest parts of Perl 6 to
10
+ design, but through persistence and patience, Perl 6 has arrived with an
11
+ elegant system for handling them.
12
12
13
13
= head1 Literal lists
14
14
@@ -219,16 +219,17 @@ to an C<@>-sigiled variable.
219
219
@s[1]; # does not say anything
220
220
@s[4]; # says 42 two more times
221
221
222
- You may also use the C < .cache > method instead of C < .list > , depending
223
- on how you want the references handled. See the L < page on C < Seq > |/type/Seq >
224
- for details.
222
+ You may also use the C < .cache > method instead of C < .list > , depending on
223
+ how you want the references handled. See the L < page on
224
+ C < Seq > |/type/Seq > for details.
225
225
226
226
= comment TODO document .iterator
227
227
228
228
= head2 Slips
229
229
230
230
Sometimes you want to insert the elements of a list into another list.
231
- This can be done with a special type of list called a L < Slip|/type/Slip > .
231
+ This can be done with a special type of list called a
232
+ L < Slip|/type/Slip > .
232
233
233
234
say (1, (2, 3), 4) eqv (1, 2, 3, 4); # OUTPUT: «False»
234
235
say (1, Slip.new(2, 3), 4) eqv (1, 2, 3, 4); # OUTPUT: «True»
@@ -329,11 +330,12 @@ to put an infinite list, lest your program hang and, eventually, run out of memo
329
330
my @a = (loop { $i.say; last unless --$i }); # OUTPUT: «321»
330
331
say "take off!";
331
332
332
- = head2 Flattening "Context "
333
+ = head2 Flattening "context "
333
334
334
- When you have a list that contains sub-lists, but you only want one flat list,
335
- you may flatten the list to produce a sequence of values as if all parentheses
336
- were removed. This works no matter how many levels deep the parentheses are nested.
335
+ When you have a list that contains sub-lists, but you only want one flat
336
+ list, you may flatten the list to produce a sequence of values as if all
337
+ parentheses were removed. This works no matter how many levels deep the
338
+ parentheses are nested.
337
339
338
340
say (1, (2, (3, 4)), 5).flat eqv (1, 2, 3, 4, 5) # OUTPUT: «True»
339
341
@@ -356,15 +358,16 @@ flattening:
356
358
357
359
When a list appears as arguments to a function or method call, special
358
360
syntax rules are at play: the list is immediately converted into a
359
- C < Capture > . A C < Capture > itself has a List (C < .list > ) and a Hash ( C < .hash > ).
360
- Any C < Pair > literals whose keys are not quoted, or which are not parenthesized,
361
- never make it into C < .list > . Instead, they are considered to be named
362
- arguments and squashed into C < .hash > . See the L < page on C < Capture > |/type/Capture >
363
- for the details of this processing.
361
+ C < Capture > . A C < Capture > itself has a List (C < .list > ) and a Hash
362
+ ( C < .hash > ). Any C < Pair > literals whose keys are not quoted, or which are
363
+ not parenthesized, never make it into C < .list > . Instead, they are
364
+ considered to be named arguments and squashed into C < .hash > . See the
365
+ L < page on C < Capture > |/type/Capture > for the details of this processing.
364
366
365
- Consider the following ways to make a new C < Array > from a C < List > . These ways
366
- place the C < List > in an argument list context and because of that, the C < Array >
367
- only contains C < 1 > and C < 2 > but not the C < Pair > C < :c(3) > , which is ignored.
367
+ Consider the following ways to make a new C < Array > from a C < List > .
368
+ These ways place the C < List > in an argument list context and because of
369
+ that, the C < Array > only contains C < 1 > and C < 2 > but not the C < Pair >
370
+ C < :c(3) > , which is ignored.
368
371
369
372
Array.new(1, 2, :c(3));
370
373
Array.new: 1, 2, :c(3);
@@ -615,33 +618,34 @@ by hand to undo the nesting:
615
618
616
619
say gather [0, [(1, 2), [3, 4]], $(5, 6)].deepmap: *.take; # OUTPUT: «(1 2 3 4 5 6)»
617
620
618
- ...future versions of Perl 6 might find a way to make this easier. However,
619
- not returning Arrays or itemized lists from functions, when non-itemized lists
620
- are sufficient, is something that one should consider as a courtesy to their
621
- users:
621
+ ... Future versions of Perl 6 might find a way to make this easier.
622
+ However, not returning Arrays or itemized lists from functions, when
623
+ non-itemized lists are sufficient, is something that one should consider
624
+ as a courtesy to their users:
622
625
623
- = item use Slips when you want to always merge with surrounding lists
624
- = item use non-itemized lists when you want to make it easy for the user to flatten
625
- = item use itemized lists to protect things the user probably will not want flattened
626
- = item use Arrays as non-itemized lists of itemized lists, if appropriate,
627
- = item use Arrays if the user is going to want to mutate the result without copying it first.
626
+ = item Use C < Slip > s when you want to always merge with surrounding lists.
627
+ = item Use non-itemized lists when you want to make it easy for the user to flatten.
628
+ = item Use itemized lists to protect things the user probably will not want flattened.
629
+ = item Use C < Arrays > as non-itemized lists of itemized lists, if appropriate.
630
+ = item Use C < Array > s if the user is going to want to mutate the result without copying it first.
628
631
629
- The fact that all elements of an array are itemized (in C < Scalar > containers)
630
- is more a gentleman's agreement than a universally enforced rule, and it is
631
- less well enforced that typechecks in typed arrays. See the section below
632
- on binding to Array slots.
632
+ The fact that all elements of an array are itemized (in C < Scalar >
633
+ containers) is more a gentleman's agreement than a universally enforced
634
+ rule, and it is less well enforced that typechecks in typed arrays. See
635
+ the section below on binding to Array slots.
633
636
634
637
= head2 Literal arrays
635
638
636
- Literal Arrays are constructed with a List inside square brackets. The List is
637
- eagerly iterated (at compile time if possible) and values in the list are each
638
- type-checked and itemized. The square brackets themselves will spill elements
639
- into surrounding lists when flattened, but the elements themselves will not
640
- spill due to the itemization.
639
+ Literal C < Array > s are constructed with a C < List > inside square brackets.
640
+ The C < List > is eagerly iterated (at compile time if possible) and values
641
+ in it are each type-checked and itemized. The square brackets
642
+ themselves will spill elements into surrounding lists when flattened,
643
+ but the elements themselves will not spill due to the itemization.
641
644
642
645
= head2 Mutability
643
646
644
- Unlike lists, Arrays are mutable. Elements may deleted, added, or changed.
647
+ Unlike lists, C < Array > s are mutable. Elements may deleted, added, or
648
+ changed.
645
649
646
650
my @a = "a", "b", "c";
647
651
@a.say; # OUTPUT: «[a b c]»
@@ -655,22 +659,23 @@ Unlike lists, Arrays are mutable. Elements may deleted, added, or changed.
655
659
656
660
= head3 Assigning
657
661
658
- Assignment of a list to an Array is eager. The list will be entirely evaluated,
659
- and should not be infinite or the program may hang. Assignment to a slice of
660
- an C < Array > is, likewise, eager, but only up to the requested number of elements,
661
- which may be finite:
662
+ Assignment of a list to an C < Array > is eager. The list will be entirely
663
+ evaluated, and should not be infinite or the program may hang.
664
+ Assignment to a slice of an C < Array > is, likewise, eager, but only up to
665
+ the requested number of elements, which may be finite:
662
666
663
667
my @a;
664
668
@a[0, 1, 2] = (loop { 42 });
665
669
@a.say; # OUTPUT: «[42 42 42]»
666
670
667
- During assignment, each value will be typechecked to ensure it is a permitted
668
- type for the C < Array > . Any C < Scalar > will be stripped from each value and a
669
- new C < Scalar > will be wrapped around it.
671
+ During assignment, each value will be typechecked to ensure it is a
672
+ permitted type for the C < Array > . Any C < Scalar > will be stripped from
673
+ each value and a new C < Scalar > will be wrapped around it.
670
674
671
675
= head3 Binding
672
676
673
- Individual Array slots may be bound the same way C < $ > -sigiled variables are:
677
+ Individual Array slots may be bound the same way C < $ > -sigiled variables
678
+ are:
674
679
675
680
my $b = "foo";
676
681
my @a = 1, 2, 3;
@@ -679,11 +684,12 @@ Individual Array slots may be bound the same way C<$>-sigiled variables are:
679
684
$b = "bar";
680
685
@a.say; # OUTPUT: «[1 2 "bar"]»
681
686
682
- ...but binding Array slots directly to values is strongly discouraged. If you do,
683
- expect surprises with built-in functions. The only time this would be done is if
684
- a mutable container that knows the difference between values and Scalar-wrapped
685
- values is needed, or for very large Arrays where a native-typed array cannot be used.
686
- Such a creature should never be passed back to unsuspecting users.
687
+ ... But binding C < Array > slots directly to values is strongly
688
+ discouraged. If you do, expect surprises with built-in functions. The
689
+ only time this would be done is if a mutable container that knows the
690
+ difference between values and Scalar-wrapped values is needed, or for
691
+ very large C < Array > s where a native-typed array cannot be used. Such a
692
+ creature should never be passed back to unsuspecting users.
687
693
688
694
= end pod
689
695
0 commit comments