You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Added the newline character to the output of the code examples
where the statement 'say' was used. Also fixed some
minor typos and changed some wording.
In this case, C<$list> is using the Scalar sigil and thus will be anC<Scalar>. Any scalar will the same value will be exactly the same, as shown when printing the pointers.
50
+
In this case, C<$list> is using the Scalar sigil and thus will be aC<Scalar>. Any scalar with the same value will be exactly the same, as shown when printing the pointers.
51
51
52
52
=head1Complex data structures
53
53
54
-
Complex data structures fall in two different broad categories
54
+
Complex data structures fall in two different broad categories:
55
55
L<Positional|/type/Positional>, or list-like and
56
56
L<Associative|/type/Associative>, or key-value pair like, according to how you
57
57
access its first-level elements. In general, complex data structures, including
58
58
objects, will be a combination of both, with object properties assimilated to
59
-
key-value pairs. While all objects subclass L<Mu>, in general complex objects are instances of subclasses of L<Any>. While it is theoretically possible to mix in C<Positional> or C<Associative> without doing so, most methods who apply to complex data structures are implemented in C<Any>.
59
+
key-value pairs. While all objects subclass L<Mu>, in general complex objects are instances of subclasses of L<Any>. While it is theoretically possible to mix in C<Positional> or C<Associative> without doing so, most methods applicable to complex data structures are implemented in C<Any>.
60
60
61
61
Navigating these complex data structures is a challenge, but Perl 6 provides a couple of functions that can be used on them: L<C<deepmap>|/routine/deepmap> and L<C<duckmap>|/routine/duckmap>. While the former will go to every single element, in order, and do whatever the block passed requires,
62
62
@@ -67,7 +67,7 @@ which returns 1 because it goes to the deeper level and applies C<elems> to
67
67
them, C<deepmap> can perform more complicated operations:
68
68
69
69
say [[1,2,[3,4]],[[5,6,[7,8]]]].duckmap:
70
-
-> $array where .elems == 2 { $array.elems }
70
+
-> $array where .elems == 2 { $array.elems };
71
71
# OUTPUT: «[[1 2 2] [5 6 2]]»
72
72
73
73
In this case, it dives into the structure, but returns the element itself if it
@@ -76,17 +76,18 @@ elements of the array if it does (the two C<2>s at the end of each subarray).
76
76
77
77
Since C<d(eep|uck)map> are C<Any> methods, they also apply to Associative arrays:
78
78
79
-
say %( first => [1,2], second => [3,4]).deepmap( *.elems )
79
+
say %( first => [1,2], second => [3,4]).deepmap( *.elems );
80
80
# OUTPUT: «{first => [1 1], second => [1 1]}»
81
81
82
82
Only in this case, they will be applied to every list or array that is a value, leaving the keys alone.
83
83
84
84
C<Positional> and C<Associative> can be turned into each other.
85
85
86
-
say %( first => [1,2], second => [3,4]).list[0]# OUTPUT: «second => [3 4]»
86
+
say %( first => [1,2], second => [3,4] ).list[0];
87
+
# OUTPUT: «second => [3 4]»
87
88
88
89
However, in this case, and for Rakudo >= 2018.05, it will return a different
89
-
value every time you run. A hash will be turned into a list of the key-value
90
+
value every time it runs. A hash will be turned into a list of the key-value
90
91
pairs, but it is guaranteed to be disordered. You can also do the operation in
91
92
the opposite direction, as long as the list has an even number of elements (odd
92
93
number will result in an error):
@@ -104,7 +105,7 @@ Complex data structures are also L<Iterable>. Generating an L<iterator> out of
104
105
them will allow the program to visit the first level of the structure, one by
105
106
one:
106
107
107
-
.say for 'א'..'ס' # OUTPUT: «אבגדהוזחטיךכלםמןנס»
108
+
.say for 'א'..'ס'; # OUTPUT: «אבגדהוזחטיךכלםמןנס»
108
109
109
110
C<'א'..'ס'> is a L<Range>, a complex data structure, and with C<for> in front it
110
111
will iterate until the list is exhausted. You can use C<for> on your complex
@@ -130,11 +131,11 @@ in most cases it is elided for the sake of simplicity; this sigil elimination is
0 commit comments