Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Pass example compilation test
  • Loading branch information
coke committed Jul 10, 2018
1 parent 979f7f9 commit 1f70c83
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions doc/Language/101-basics.pod6
Expand Up @@ -17,6 +17,7 @@ winner.
The input data (stored in a file called "scores.txt") looks like this:
=for code :lang<data>
Beth Ana Charlie Dave
Ana Dave | 3:0
Charlie Beth | 3:1
Expand Down Expand Up @@ -63,6 +64,7 @@ Here's one way to solve that problem in Perl 6:
This produces the output:
=for code :lang<data>
Ana has won 2 matches and 8 sets
Dave has won 2 matches and 6 sets
Charlie has won 1 matches and 4 sets
Expand Down Expand Up @@ -126,7 +128,7 @@ C<'scores.txt'> is a I<string literal>. A string is a piece of text, and a strin
literal is a string which appears directly in the program. In this line, it's
the argument provided to C<open>.
=begin code
=begin code :preamble<my $file>
my @names = $file.get.words;
=end code
Expand Down Expand Up @@ -165,7 +167,7 @@ In the score counting program, C<%matches> stores the number of matches each
player has won. C<%sets> stores the number of sets each player has won. Both
of these hashes are indexed by the player's name.
=begin code
=begin code :preamble<my $file>
for $file.lines -> $line {
...
}
Expand All @@ -182,7 +184,7 @@ since we already called C<$file.get> once, and going all the way to the end of t
During the first iteration, C<$line> will contain the string C<Ana Dave |
3:0>; during the second, C<Charlie Beth | 3:1>, and so on.
=begin code
=begin code :preamble<my $line>
my ($pairing, $result) = $line.split(' | ');
=end code
Expand All @@ -202,7 +204,7 @@ and C<$result> C<3:0>.
The next two lines follow the same pattern:
=begin code
=begin code :preamble<my $pairing;my $result>
my ($p1, $p2) = $pairing.words;
my ($r1, $r2) = $result.split(':');
=end code
Expand All @@ -227,14 +229,14 @@ cell C<'0'>
The program then counts the number of sets each player has won:
=begin code
=begin code :preamble<my %sets;my $p1;my $p2;my $r1;my $r2>
%sets{$p1} += $r1;
%sets{$p2} += $r2;
=end code
The C<+=> assignment operator is a shortcut for:
=begin code
=begin code :preamble<my %sets;my $p1;my $p2;my $r1;my $r2>
%sets{$p1} = %sets{$p1} + $r1;
%sets{$p2} = %sets{$p2} + $r2;
=end code
Expand All @@ -255,7 +257,7 @@ value starting at zero. (This is I<autovivification>). After these two lines
have run for the first time, C<%sets> contains C<< 'Ana' => 3, 'Dave' => 0 >>.
(The fat arrow C<< => >> separates key and value in a C<Pair>.)
=begin code
=begin code :preamble<my $r1;my $r2;my $p1;my $p2;my %matches;>
if $r1 > $r2 {
%matches{$p1}++;
} else {
Expand All @@ -276,7 +278,7 @@ the increment, not the incremented value. As in many other
programming languages, you can use C<++> as a prefix. Then it returns the
incremented value; C<my $x = 1; say ++$x> prints C<2>.
=begin code
=begin code :preamble<my @names;my %sets;my %matches>
my @sorted = @names.sort({ %sets{$_} }).sort({ %matches{$_} }).reverse;
=end code
Expand Down Expand Up @@ -319,7 +321,7 @@ C<sort> sorts in ascending order, from smallest to largest. This is the
opposite of the desired order. Therefore, the code calls the C<.reverse> method
on the result of the second sort, and stores the final list in C<@sorted>.
=begin code
=begin code :preamble<my @sorted;my %matches;my %sets>
for @sorted -> $n {
say "$n has won %matches{$n} matches and %sets{$n} sets";
}
Expand Down Expand Up @@ -374,7 +376,7 @@ to make a list of strings. This is the C<< <...> >> L<quote-words|https://docs.p
When you put words in between the < and > they are all assumed to be strings,
so you do not need to wrap them each in double quotes C<< "..." >>.
=begin code
=begin code :preamble<my %sets>
say "Math: { 1 + 2 }"; # Math: 3
my @people = <Luke Matthew Mark>;
say "The synoptics are: {@people}"; # The synoptics are: Luke Matthew Mark
Expand Down Expand Up @@ -425,13 +427,13 @@ Hint: C<%hash.keys> returns a list of all keys stored in C<%hash>.
B<Answer:> Remove the line C<my @names = $file.get.words;>, and change:
=begin code
=begin code :preamble<my @names;my %sets;my %matches>
my @sorted = @names.sort({ %sets{$_} }).sort({ %matches{$_} }).reverse;
=end code
... into:
=begin code
=begin code :preamble<my %sets;my %matches>
my @sorted = %sets.keys.sort({ %sets{$_} }).sort({ %matches{$_} }).reverse;
=end code
Expand All @@ -445,10 +447,10 @@ B<Answer:> Change C<@names> to C<@valid-players>. When looping through the lines
the file, check to see that C<$p1> and C<$p2> are in C<@valid-players>. Note that
for membership operators you can also use C<(elem)> and C<!(elem)>.
=begin code
...
=begin code :preamble<my $file>
...;
my @valid-players = $file.get.words;
...
...;
for $file.lines -> $line {
my ($pairing, $result) = $line.split(' | ');
Expand Down

0 comments on commit 1f70c83

Please sign in to comment.