Skip to content

Commit f1881ff

Browse files
committed
Adds complex data structres advances #114
1 parent 47d87c2 commit f1881ff

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

doc/Language/structures.pod6

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,53 @@ In this case, C<$list> is using the Scalar sigil and thus will be an C<Scalar>.
5151
5252
=head1 Complex data structures
5353
54-
Complex data structures fall in two different broad categories L<Positional|/type/Positional>, or list-like and L<Associative|/type/Associative>, or key-value pair like, according to how you access its first-level elements. In general, complex data structures, including objects, will be a combination of both, with object properties assimilated to key-value pairs.
54+
Complex data structures fall in two different broad categories
55+
L<Positional|/type/Positional>, or list-like and
56+
L<Associative|/type/Associative>, or key-value pair like, according to how you
57+
access its first-level elements. In general, complex data structures, including
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>.
5560
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,
5662
63+
say [[1,2,[3,4]],[[5,6,[7,8]]]].deepmap( *.elems );
64+
# OUTPUT: «[[1 1 [1 1]] [1 1 [1 1]]]␤»
65+
66+
which returns 1 because it goes to the deeper level and applies C<elems> to
67+
them, C<deepmap> can perform more complicated operations:
68+
69+
say [[1,2,[3,4]],[[5,6,[7,8]]]].duckmap:
70+
-> $array where .elems == 2 { $array.elems }
71+
# OUTPUT: «[[1 2 2] [5 6 2]]␤»
72+
73+
In this case, it dives into the structure, but returns the element itself if it
74+
does not meet the condition in the block (C<1,2>), returning the number of
75+
elements of the array if it does (the two C<2>s at the end of each subarray).
76+
77+
Since C<d(eep|uck)map> are C<Any> methods, they also apply to Associative arrays:
78+
79+
say %( first => [1,2], second => [3,4]).deepmap( *.elems )
80+
# OUTPUT: «{first => [1 1], second => [1 1]}␤»
81+
82+
Only in this case, they will be applied to every list or array that is a value, leaving the keys alone.
83+
84+
C<Positional> and C<Associative> can be turned into each other.
85+
86+
say %( first => [1,2], second => [3,4]).list[0]# OUTPUT: «second => [3 4]␤»
87+
88+
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+
pairs, but it is guaranteed to be disordered. You can also do the operation in
91+
the opposite direction, as long as the list has an even number of elements (odd
92+
number will result in an error):
93+
94+
say <a b c d>.Hash # OUTPUT: «{a => b, c => d}␤»
95+
96+
But
97+
98+
say <a b c d>.Hash.kv # OUTPUT: «(c d a b)␤»
99+
100+
will obtain a different value every time you run it; L<C<kv>|/type/Pair#method_kv> turns every C<Pair> into a list.
57101
58102
=head1 Functional structures
59103

0 commit comments

Comments
 (0)