Skip to content

Commit ed55bff

Browse files
committed
Minor corrections to the data structure doc
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.
1 parent 9afe5ee commit ed55bff

File tree

1 file changed

+44
-44
lines changed

1 file changed

+44
-44
lines changed

doc/Language/structures.pod6

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
77
=head1 Scalar structures
88
9-
Some classes do not have any I<internal> structure, and to access parts
10-
of them specific methods have to be used. Numbers, strings, and some
9+
Some classes do not have any I<internal> structure and to access parts
10+
of them, specific methods have to be used. Numbers, strings, and some
1111
other monolithic classes are included in that class. They use the C<$>
1212
sigil, although complex data structures can also use it.
1313
@@ -34,7 +34,7 @@ An interesting side effect, or maybe intended feature, is that scalarization
3434
conserves identity of complex structures.
3535
3636
for ^2 {
37-
my @list =(1,1);
37+
my @list = (1,1);
3838
say @list.WHICH;
3939
} # OUTPUT: «Array|93947995146096␤Array|93947995700032␤»
4040
@@ -43,20 +43,20 @@ in the sense that C<===> will say it is; as it is shown, different values of the
4343
internal pointer representation are printed. However
4444
4545
for ^2 {
46-
my $list =(1,1);
46+
my $list = (1,1);
4747
say $list.WHICH
48-
} # OUTPUT: «List|94674814008432␤List|94674814008432␤»
48+
} # OUTPUT: «List|94674814008432␤List|94674814008432␤»
4949
50-
In this case, C<$list> is using the Scalar sigil and thus will be an C<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 a C<Scalar>. Any scalar with the same value will be exactly the same, as shown when printing the pointers.
5151
5252
=head1 Complex data structures
5353
54-
Complex data structures fall in two different broad categories
54+
Complex data structures fall in two different broad categories:
5555
L<Positional|/type/Positional>, or list-like and
5656
L<Associative|/type/Associative>, or key-value pair like, according to how you
5757
access its first-level elements. In general, complex data structures, including
5858
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>.
6060
6161
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,
6262
@@ -67,7 +67,7 @@ which returns 1 because it goes to the deeper level and applies C<elems> to
6767
them, C<deepmap> can perform more complicated operations:
6868
6969
say [[1,2,[3,4]],[[5,6,[7,8]]]].duckmap:
70-
-> $array where .elems == 2 { $array.elems }
70+
-> $array where .elems == 2 { $array.elems };
7171
# OUTPUT: «[[1 2 2] [5 6 2]]␤»
7272
7373
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).
7676
7777
Since C<d(eep|uck)map> are C<Any> methods, they also apply to Associative arrays:
7878
79-
say %( first => [1,2], second => [3,4]).deepmap( *.elems )
79+
say %( first => [1,2], second => [3,4] ).deepmap( *.elems );
8080
# OUTPUT: «{first => [1 1], second => [1 1]}␤»
8181
8282
Only in this case, they will be applied to every list or array that is a value, leaving the keys alone.
8383
8484
C<Positional> and C<Associative> can be turned into each other.
8585
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]␤»
8788
8889
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
9091
pairs, but it is guaranteed to be disordered. You can also do the operation in
9192
the opposite direction, as long as the list has an even number of elements (odd
9293
number will result in an error):
@@ -104,7 +105,7 @@ Complex data structures are also L<Iterable>. Generating an L<iterator> out of
104105
them will allow the program to visit the first level of the structure, one by
105106
one:
106107
107-
.say for 'א'..'ס' # OUTPUT: «א␤ב␤ג␤ד␤ה␤ו␤ז␤ח␤ט␤י␤ך␤כ␤ל␤ם␤מ␤ן␤נ␤ס␤»
108+
.say for 'א'..'ס'; # OUTPUT: «א␤ב␤ג␤ד␤ה␤ו␤ז␤ח␤ט␤י␤ך␤כ␤ל␤ם␤מ␤ן␤נ␤ס␤»
108109
109110
C<'א'..'ס'> is a L<Range>, a complex data structure, and with C<for> in front it
110111
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
130131
always allowed in the case of C<Callables>.
131132
132133
my &a-func= { (^($^þ)).Seq };
133-
say a-func(3), a-func(7)# OUTPUT: «(0 1 2)(0 1 2 3 4 5 6)␤»
134+
say a-func(3), a-func(7); # OUTPUT: «(0 1 2)(0 1 2 3 4 5 6)␤»
134135
135136
L<Block>s are the simplest callable structures, since C<Callable>s cannot be
136137
instantiated. In this case we implement a block that logs events and can
137-
retrieve them
138+
retrieve them:
138139
139140
my $logger = -> $event, $key = Nil {
140141
state %store;
@@ -145,38 +146,37 @@ retrieve them
145146
}
146147
}
147148
$logger( "Stuff" );
148-
$logger( "More stuff");
149-
say $logger( Nil, "2018-05-28"); # OUTPUT: «(Stuff More stuff)»
150-
»
149+
$logger( "More stuff" );
150+
say $logger( Nil, "2018-05-28" ); # OUTPUT: «(Stuff More stuff)␤»
151151
152152
A C<Block> has a L<Signature>, in this case two arguments, the first of
153-
which is the event that is going ot be logged, and the second is the key
153+
which is the event that is going to be logged, and the second is the key
154154
to retrieve the events. They will be used in an independent way, but its
155155
intention is to showcase the use of a L<state variable|/syntax/state>
156156
that is kept from every invocation to the next. This state variable is
157157
encapsulated within the block, and cannot be accessed from outside
158158
except by using the simple API the block provides: calling the block
159159
with a second argument. The two first invocations log two events, the
160160
third invocation at the bottom of the example use this second type of
161-
call to retrieve the stored values. C<Block>s can be cloned
161+
call to retrieve the stored values. C<Block>s can be cloned:
162162
163-
=begin code :preamble<my $logger = sub ( $a, $b ){ return $a, $b }>
163+
=begin code :preamble<my $logger = sub ( $a, $b ){ return $a, $b }>
164164
my $clogger = $logger.clone;
165165
$clogger( "Clone stuff" );
166-
$clogger( "More clone stuff");
167-
say $clogger( Nil, "2018-05-28");
168-
# OUTPUT: «(Clone stuff More clone stuff)»
166+
$clogger( "More clone stuff" );
167+
say $clogger( Nil, "2018-05-28" );
168+
# OUTPUT: «(Clone stuff More clone stuff)»
169169
=end code
170170
171171
And cloning will reset the state variable; instead of cloning, we can create
172172
I<façades> that change the API. For instance, eliminate the need to use C<Nil>
173173
as first argument to retrieve the log for a certain date:
174174
175-
=begin code :preamble<my $logger = sub ( $a, $b ){ return $a, $b }>
175+
=begin code :preamble<my $logger = sub ( $a, $b ){ return $a, $b }>
176176
my $gets-logs = $logger.assuming( Nil, * );
177177
$logger( %(changing => "Logs") );
178178
say $gets-logs( "2018-05-28" );
179-
# OUTPUT: «({changing => Logs} Stuff More stuff)»
179+
# OUTPUT: «({changing => Logs} Stuff More stuff)»
180180
=end code
181181
182182
L<C<assuming>|/type/Block#(Code)_method_assuming> wraps around a block
@@ -187,13 +187,13 @@ are calling C<$logger> I<assuming> the first argument is C<Nil>". We can
187187
slightly change the appearance of these two Blocks to clarify they are
188188
actually acting on the same block:
189189
190-
=begin code :preamble<my $logger = sub ( $a, $b ){ return $a, $b }>
190+
=begin code :preamble<my $logger = sub ( $a, $b ){ return $a, $b }>
191191
my $Logger = $logger.clone;
192192
my $Logger::logs = $Logger.assuming( *, Nil );
193193
my $Logger::get = $Logger.assuming( Nil, * );
194194
$Logger::logs( <an array> );
195-
$Logger::logs( %(key => 42 ) );
196-
say $Logger::get( "2018-05-28");
195+
$Logger::logs( %(key => 42) );
196+
say $Logger::get( "2018-05-28" );
197197
=end code
198198
199199
Although C<::> is generally used for invocation of class methods, it is
@@ -213,7 +213,7 @@ As such first class data structures, callables can be used anywhere another type
213213
214214
Regexes are actually a type of callable:
215215
216-
say /regex/.does( Callable );#OUTPUT: «True␤»
216+
say /regex/.does( Callable ); # OUTPUT: «True␤»
217217
218218
And in the example above we are calling regexes stored in an array, and
219219
applying them to a string literal.
@@ -223,14 +223,14 @@ Callables are composed by using the L<function composition operator ∘|/languag
223223
=begin code :preamble<my $Logger::logs = sub ( $a ) { $a }>
224224
my $typer = -> $thing { $thing.^name ~ ' → ' ~ $thing };
225225
my $Logger::withtype = $Logger::logs ∘ $typer;
226-
$Logger::withtype( Pair.new( 'left', 'right' ));
226+
$Logger::withtype( Pair.new( 'left', 'right' ) );
227227
$Logger::withtype( ¾ );
228228
say $Logger::get( "2018-05-28" );
229-
# OUTPUT: «(Pair → left right Rat → 0.75)»
229+
# OUTPUT: «(Pair → left right Rat → 0.75)»
230230
=end code
231231
232232
We are composing C<$typer> with the C<$Logger::logs> function defined
233-
above, obtaining a function that logs an object prececed by its type,
233+
above, obtaining a function that logs an object preceded by ts type,
234234
which can be useful for filtering, for instance. C<$Logger::withtype>
235235
is, in fact, a complex data structure composed of two functions which
236236
are applied in a serial way, but every one of the callables composed can
@@ -248,10 +248,10 @@ mixes roles or values into a value or a variable:
248248
=begin code
249249
my %not-scalar := %(2 => 3) but Associative[Int,Int];
250250
say %not-scalar.^name; # OUTPUT: «Hash+{Associative[Int,Int]}␤»
251-
say %not-scalar.of; # Associative[Int,Int]»
252-
%not-scalar{3}=4;
253-
%not-scalar<thing>=3;
254-
say %not-scalar; # OUTPUT: «{2 => 3, 3 => 4, thing => 3}␤»
251+
say %not-scalar.of; # OUTPUT: «Associative[Int,Int]»
252+
%not-scalar{3} = 4;
253+
%not-scalar<thing> = 3;
254+
say %not-scalar; # OUTPUT: «{2 => 3, 3 => 4, thing => 3}␤»
255255
=end code
256256
257257
In this case, C<but> is mixing in the C<Associative[Int,Int]> role;
@@ -275,8 +275,8 @@ role Lastable {
275275
}
276276
}
277277
my %hash-plus := %( 3 => 33, 4 => 44) but Lastable;
278-
say %hash-plus.sort[0]; # OUTPUT: «3 => 33»
279-
say %hash-plus.last; # OUTPUT: «4 => 44»
278+
say %hash-plus.sort[0]; # OUTPUT: «3 => 33»
279+
say %hash-plus.last; # OUTPUT: «4 => 44»
280280
=end code
281281
282282
In C<Lastable> we use the universal C<self> variable to refer to
@@ -303,7 +303,7 @@ say $one-fraction; # OUTPUT: «0.333333␤»
303303
On the other hand, C<my OneOver $ = ⅔;> will cause a type-check error.
304304
Subsets can use C<Whatever>, that is, C<*>, to refer to the argument;
305305
but this will be instantiated every time you use it to a different
306-
argument, so if we used it twice in the definition we would get an
306+
argument, so if we use it twice in the definition we would get an
307307
error. In this case we are using the topic single variable, C<$_>, to
308308
check the instantiation. Subsetting can be done directly, without the
309309
need of declaring it, in L<signatures|/language/typesystem#subset>.
@@ -420,7 +420,7 @@ mentioned above:
420420
=for code
421421
my $fh = "/tmp/bar".IO.open;
422422
my $lines = $fh.lines;
423-
say "Read $lines.elems() lines"; #reifying before closing handle
423+
say "Read $lines.elems() lines"; # reifying before closing handle
424424
close $fh;
425425
say $lines[0]; # no problem!
426426
@@ -438,13 +438,13 @@ Languages that allow
438438
L<introspection|https://en.wikipedia.org/wiki/Introspection> like Perl 6
439439
have functionalities attached to the type system that let the developer
440440
access container and value metadata. This metadata can be used in a
441-
program to carry out different actions depending on their value. As is
441+
program to carry out different actions depending on their value. As it is
442442
obvious from the name, metadata are extracted from a value or container
443443
via the metaclass.
444444
445445
my $any-object = "random object";
446446
my $metadata = $any-object.HOW;
447-
say $metadata.^mro; # OUTPUT: «((ClassHOW) (Any) (Mu))␤
447+
say $metadata.^mro; # OUTPUT: «((ClassHOW) (Any) (Mu))␤»
448448
say $metadata.can( $metadata, "uc" ); # OUTPUT: «(uc uc)␤»
449449
450450
With the first C<say> we show the class hierarchy of the metamodel class, which
@@ -458,7 +458,7 @@ other cases, when roles are mixed in directly into a variable. For instance, in
458458
the L<case of C<%hash-plus> defined above|#Defining_and_constraining_data_structures>:
459459
460460
=for code :preamble<my %hash-plus>
461-
say %hash-plus.^can("last"); # OUTPUT «(last)␤»
461+
say %hash-plus.^can("last"); # OUTPUT: «(last)␤»
462462
463463
In this case we are using the I<syntactic sugar> for C<HOW.method>, C<^method>,
464464
to check if your data structure responds to that method; the output, which shows

0 commit comments

Comments
 (0)