Skip to content

Commit 1896846

Browse files
authored
Merge pull request #3457 from uzluisf/master
Revise 'Raku by example 101' page
2 parents 5bdc659 + d4e0599 commit 1896846

File tree

1 file changed

+72
-59
lines changed

1 file changed

+72
-59
lines changed

doc/Language/101-basics.pod6

Lines changed: 72 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
=begin pod :kind("Language") :subkind("Language") :category("beginning")
22
3-
=TITLE Raku by example P6-101
3+
=TITLE Raku by example 101
44
55
=SUBTITLE A basic introductory example of a Raku program
66
@@ -29,20 +29,15 @@ Here's one way to solve that problem in Raku:
2929
=begin code :solo
3030
use v6;
3131
32-
# Single line comments like this one start with '#'.
33-
my $file = open 'scores.txt'; # Comments can also follow code on a line.
34-
my @names = $file.get.words;
32+
# start by printing out the header.
33+
say "Tournament Results:\n";
34+
35+
my $file = open 'scores.txt'; # get filehandle and...
36+
my @names = $file.get.words; # ... get players.
3537
3638
my %matches;
3739
my %sets;
3840
39-
#`( Multiple line comments
40-
are denoted by a #, then `, then one of {,[, or (
41-
and closed by the corresponding },], and ).
42-
Nested pairs of square brackets, curly braces and
43-
parentheses are matched, so
44-
something like this () will not end the comment.
45-
In this case, closed by: )
4641
for $file.lines -> $line {
4742
next unless $line; # ignore any empty lines
4843
@@ -60,9 +55,6 @@ for $file.lines -> $line {
6055
}
6156
}
6257
63-
#`[
64-
This is another multi-line comment. ]
65-
#`{ So is this, though it's not actually multi-line. }
6658
my @sorted = @names.sort({ %sets{$_} }).sort({ %matches{$_} }).reverse;
6759
6860
for @sorted -> $n {
@@ -74,37 +66,55 @@ for @sorted -> $n {
7466
7567
This produces the output:
7668
77-
=for code :lang<text>
69+
=begin code :lang<text>
70+
Tournament Results:
71+
7872
Ana has won 2 matches and 8 sets
7973
Dave has won 2 matches and 6 sets
8074
Charlie has won 1 match and 4 sets
8175
Beth has won 1 match and 4 sets
76+
=end code
8277
83-
=head3 X<C<v6>|v6 (Basics)>
78+
=head1 Pragma X<C<v6>|v6 (Basics)>
79+
80+
=begin code
81+
use v6;
82+
=end code
8483
8584
Every Raku program should begin with a line similar to C<use v6;>. This line
8685
tells the compiler which version of Raku the program expects. For instance,
8786
6.c is an example of a Raku version. Should you accidentally run the file
8887
with Perl, you'll get a helpful error message.
8988
90-
=head3 X<C<statement>|statement (Basics)>
89+
=head1 X<Statements|statement (Basics)>
90+
91+
=begin code
92+
# start by printing out the header.
93+
say "Tournament Results:\n";
94+
=end code
9195
9296
A Raku program consists of zero or more statements. A I<statement> ends with
93-
a semicolon or a curly brace at the end of a line:
97+
a semicolon or a curly brace at the end of a line.
98+
99+
In Raku, single line comments start with a single hash character C<#> and
100+
extend until the end of the line. Raku also supports
101+
L<multi-line/embedded comments|/language/syntax#Multi-line_/_embedded_comments>.
102+
The compiler doesn't evaluate comments as program text and they're only intended
103+
for human readers.
104+
105+
=head1 X<Lexical scope|my (Basics)> and X<block>
94106
95107
=begin code
96108
my $file = open 'scores.txt';
97109
=end code
98110
99-
=head3 X<C<lexical>> and X<C<block>>
100-
101111
C<my> declares a lexical variable, which are visible only in the current block
102112
from the point of declaration to the end of the block. If there's no enclosing
103113
block, it's visible throughout the remainder of the file (which would
104114
effectively be the enclosing block). A block is any part of the code enclosed
105115
between curly braces C<{ }>.
106116
107-
=head3 X<C<sigil>> and X<C<identifier>|identifier (Basics)>
117+
=head2 X<Sigils|sigils (Basics)> and X<identifiers|identifiers (Basics)>
108118
109119
A variable name begins with a I<sigil>, which is a non-alpha-numeric
110120
symbol such as C<$>, C<@>, C<%>, or C<&> E<mdash> or occasionally the double
@@ -115,7 +125,7 @@ consist of letters, digits and the underscore. Between letters you can
115125
also use a dash C<-> or an apostrophe C<'>, so C<isn't> and
116126
C<double-click> are valid identifiers.
117127
118-
=head3 X<C<scalar>>
128+
=head2 X<Scalar|scalar (Basics)>
119129
120130
Sigils indicate the default access method for a variable. Variables
121131
with the C<@> sigil are accessed positionally; variables with the C<%>
@@ -126,25 +136,25 @@ C<Array> or a C<Hash>; the C<$> sigil signifies that it should be
126136
treated as a single value, even in a context that expects multiple
127137
values (as with an C<Array> or C<Hash>).
128138
129-
=head3 X<C<filehandle>> and X<C<assignment>>
139+
=head2 X<Filehandle|filehandle (Basics)> and X<assignment|assignment (Basics)>
130140
131141
The built-in function C<open> opens a file, here named C<scores.txt>,
132142
and returns a I<filehandle> E<mdash> an object representing that file. The
133-
equality sign C<=> I<assigns> that filehandle to the variable on the left,
143+
assignment operator C<=> I<assigns> that filehandle to the variable on the left,
134144
which means that C<$file> now stores the filehandle.
135145
136-
=head3 X<C<string literal>>
146+
=head2 X<String literals|string literal (Basics)>
137147
138148
C<'scores.txt'> is a I<string literal>. A string is a piece of text, and a
139149
string literal is a string which appears directly in the program. In this line,
140150
it's the argument provided to C<open>.
141151
142-
=begin code :preamble<my $file>
152+
=head1 X<Arrays|array (Basics)>, X<methods|method (Basics)> and X<invocants|invocant (Basics)>
153+
154+
=begin code :preamble<my @names>
143155
my @names = $file.get.words;
144156
=end code
145157
146-
=head3 X<C<array>>, X<C<method>|method (Basics)> and X<C<invocant>|invocant (Basics)>
147-
148158
The right-hand side calls a I<method> E<mdash> a named group of
149159
behavior E<mdash> named C<get> on the filehandle stored in C<$file>. The C<get>
150160
method reads and returns one line from the file, removing the line ending. If
@@ -159,13 +169,13 @@ Finally, this list gets stored in the L<Array|/type/Array> C<@names>.
159169
The C<@> sigil marks the declared variable as an C<Array>.
160170
Arrays store ordered lists.
161171
172+
=head1 X<Hashes|hash (Basics)>
173+
162174
=begin code
163175
my %matches;
164176
my %sets;
165177
=end code
166178
167-
=head3 X<C<hash>>
168-
169179
These two lines of code declare two hashes. The C<%> sigil marks each variable
170180
as a C<Hash>. A C<Hash> is an unordered collection of key-value pairs. Other
171181
programming languages call that a I<hash table>, I<dictionary>, or I<map>.
@@ -176,14 +186,14 @@ In the score counting program, C<%matches> stores the number of matches each
176186
player has won. C<%sets> stores the number of sets each player has won. Both
177187
of these hashes are indexed by the player's name.
178188
189+
=head1 X<C<for>|for (Basics)> and X<blocks|block (Basics)>
190+
179191
=begin code :preamble<my $file>
180192
for $file.lines -> $line {
181193
...
182194
}
183195
=end code
184196
185-
=head3 X<C<for>|for (Basics)> and X<C<block>>
186-
187197
C<for> produces a loop that runs the I<block> delimited by curly braces
188198
once for each item of the list, setting the variable C<$line>
189199
to the current value of each iteration. C<$file.lines> produces a list of
@@ -259,24 +269,30 @@ there is no need to include a separate statement like C<%sets{$p1} = 0> before
259269
you can meaningfully add C<$r1> to it. We'll look at this is behavior in a bit
260270
more detail below.
261271
272+
=head2 X<C<Any>|Any (Basics)> and X<C<+=>|+= (Basics)>
262273
263-
=head3 X<C<Any>| Any (Basics)> and X<C<+=>>
274+
=begin code :preamble<my %sets;my $p1;my $p2;my $r1;my $r2>
275+
%sets{$p1} += $r1;
276+
%sets{$p2} += $r2;
277+
=end code
264278
265-
C<+= $r1> means I<increase the value in the variable on the left by $r1>. In the
266-
first iteration C<%sets{$p1}> is not yet defined, so it defaults to a special
279+
C<%sets{$p1} += $r1> means I<increase the value in the variable on the left by $r1>.
280+
In the first iteration C<%sets{$p1}> is not yet defined, so it defaults to a special
267281
value called C<Any>. The C<+=> operator conveniently treats the undefined value
268282
C<Any> as a number with the value C<0>, allowing it to sensibly add some other
269283
value to it. To perform the addition, the strings C<$r1>, C<$r2>, etc. are
270284
automatically converted to numbers, as addition is a numeric operation.
271285
272-
=head3 X<C<fat arrow>>, X<C<pair>> and X<C<autovivification>>
286+
=head2 X<Fat arrow|fat arrow (Basics)>, X<pairs|pair (Basics)> and X<autovivification|autovivification (Basics)>
273287
274288
Before these two lines execute, C<%sets> is empty. Adding to an entry that is
275289
not in the hash yet will cause that entry to spring into existence
276-
just-in-time, with a value starting at zero. (This is I<autovivification>).
277-
After these two lines have run for the first time, C<%sets> contains C<< 'Ana'
278-
=> 3, 'Dave' => 0 >>. (The fat arrow C<< => >> separates the key and the value
279-
in a C<Pair>.)
290+
just-in-time, with a value starting at zero. This behavior is known as
291+
I<autovivification>. After these two lines have run for the first time, C<%sets>
292+
contains C«'Ana' => 3, 'Dave' => 0». (The fat arrow C«=>» separates the
293+
key and the value in a C<Pair>.)
294+
295+
=head2 X<Postincrement|postincrement (Basics)> and X<preincrement|preincrement (Basics)>
280296
281297
=begin code :preamble<my $r1;my $r2;my $p1;my $p2;my %matches;>
282298
if $r1 > $r2 {
@@ -291,20 +307,18 @@ If C<$r1> is not larger than C<$r2>, C<%matches{$p2}> increments. Just as in
291307
the case of C<+=>, if either hash value did not exist previously, it is
292308
autovivified by the increment operation.
293309
294-
=head3 X<C<postincrement>> and X<C<preincrement>>
295-
296310
C<$thing++> is a variant of C<$thing += 1>; it differs from the latter in that
297311
the return value of the expression is C<$thing> I<before> the increment, and not
298312
the incremented value. As in many other programming languages, you can use C<++>
299313
as a prefix. Then it returns the incremented value: C<my $x = 1; say ++$x>
300314
prints C<2>.
301315
316+
=head1 X<Topic variable|topic variable (Basics)>
317+
302318
=begin code :preamble<my @names;my %sets;my %matches>
303319
my @sorted = @names.sort({ %sets{$_} }).sort({ %matches{$_} }).reverse;
304320
=end code
305321
306-
=head3 X<C<variables, $_>>
307-
308322
This line consists of three individually simple steps. An array's C<sort>
309323
method returns a sorted version of the array's contents. However, the default
310324
sort on an array sorts by its contents. To print player names in winner-first
@@ -313,7 +327,7 @@ names. The C<sort> method's argument is a I<block> used to transform the array
313327
elements (the names of players) to the data by which to sort. The array items
314328
are passed in through the I<topic variable> C<$_>.
315329
316-
=head3 X<C<block>|block (Basics)>
330+
=head2 Blocks
317331
318332
You have seen blocks before: both the C<for> loop C<< -> $line { ... } >> and
319333
the C<if> statement worked on blocks. A block is a self-contained piece of
@@ -325,13 +339,13 @@ have both won two matches. That simple sort doesn't account for the number of
325339
sets won, which is the secondary criterion to decide who has won the
326340
tournament.
327341
328-
=head3 X<C<stable sort>>
342+
=head2 Stable sort
329343
330344
When two array items have the same value, C<sort> leaves them in the same order
331345
as it found them. Computer scientists call this a I<stable sort>. The program
332346
takes advantage of this property of Raku's C<sort> to achieve the goal by
333-
sorting twice: first by the number of sets won (the secondary criterion), then
334-
by the number of matches won.
347+
sorting twice: first by the number of sets won (the primary criterion), then
348+
by the number of matches won (the secondary criterion).
335349
336350
After the first sorting step, the names are in the order C<Beth Charlie Dave
337351
Ana>. After the second sorting step, it's still the same, because no one has
@@ -342,6 +356,8 @@ C<sort> sorts in ascending order, from smallest to largest. This is the
342356
opposite of the desired order. Therefore, the code calls the C<.reverse> method
343357
on the result of the second sort, and stores the final list in C<@sorted>.
344358
359+
=head1 Standard output
360+
345361
=begin code :preamble<my @sorted;my %matches;my %sets>
346362
for @sorted -> $n {
347363
my $match-noun = %matches{$n} == 1 ?? 'match' !! 'matches';
@@ -350,8 +366,6 @@ for @sorted -> $n {
350366
}
351367
=end code
352368
353-
=head3 X<C<say>| say (Basics)>, X<C<print>| say (Basics)> and X<C<put>|put (Basics)>
354-
355369
To print out the players and their scores, the code loops over C<@sorted>,
356370
setting C<$n> to the name of each player in turn. Read this code as "For each
357371
element of sorted, set C<$n> to the element, then execute the contents of the
@@ -370,16 +384,16 @@ want the newline at the end.)
370384
Note that C<say> will truncate certain data structures by calling the C<.gist>
371385
method so C<put> is safer if you want exact output.
372386
373-
=head3 X<C<interpolation>>
387+
=head2 X<Variable interpolation|variable interpolation (Basics)>
374388
375389
When you run the program, you'll see that C<say> doesn't print the contents of
376390
that string verbatim. In place of C<$n> it prints the contents of the variable
377-
C<$n> E<mdash> the names of players stored in C<$n>. This automatic substitution
378-
of code with its contents is I<interpolation>. This interpolation happens only
379-
in strings delimited by double quotes C<"...">. Single quoted strings C<'...'>
380-
do not interpolate:
391+
C<$n> E<mdash> a player's name stored in C<$n>. This automatic substitution
392+
of code with its contents is called I<interpolation>. This interpolation happens
393+
only in strings delimited by double quotes C<"...">. Single quoted strings
394+
C<'...'> do not interpolate:
381395
382-
=head3 X<C<double-quoted strings>> and X<C<single-quoted strings>>
396+
=head2 X<Double-quoted strings|double-quoted strings (Basics)> and X<single-quoted strings|single-quoted strings (Basics)>
383397
384398
=begin code
385399
my $names = 'things';
@@ -389,7 +403,7 @@ say "Do not call me $names"; # OUTPUT: «Do not call me things␤»
389403
390404
Double quoted strings in Raku can interpolate variables with the C<$>
391405
sigil as well as blocks of code in curly braces. Since any arbitrary
392-
Perl code can appear within curly braces, C<Array>s and C<Hash>es may be
406+
Raku code can appear within curly braces, C<Array>s and C<Hash>es may be
393407
interpolated by placing them within curly braces.
394408
395409
Arrays within curly braces are interpolated with a single space character
@@ -429,7 +443,7 @@ followed by a postcircumfix E<mdash> a bracketing pair that follows a
429443
statement. It's also ok to have a method call between the variable name and
430444
the postcircumfix.
431445
432-
=head3 X<C<Zen slice>| Zen slice (Basics)>
446+
=head1 Zen slices
433447
434448
=begin code
435449
my @flavors = <vanilla peach>;
@@ -447,7 +461,6 @@ say "we have @flavors.sort.join(', ')";
447461
# OUTPUT: «we have peach, vanilla␤»
448462
=end code
449463
450-
451464
=head1 Exercises
452465
453466
B<1.> The input format of the example program is redundant: the first line

0 commit comments

Comments
 (0)