Skip to content

Commit 73d7182

Browse files
authored
Merge pull request #3429 from uzluisf/master
Minor rewording with few examples Thanks a lot!
2 parents b788498 + a2a3ec7 commit 73d7182

File tree

1 file changed

+47
-21
lines changed

1 file changed

+47
-21
lines changed

doc/Language/control.pod6

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ also be written as:
2424
=head1 X<Blocks|control flow, blocks>
2525
2626
Like many other languages, Raku uses C<blocks> enclosed by C<{> and C<}> to
27-
turn a sequence of statements into a single one. It is OK to skip the semicolon
27+
turn a sequence of statements into a single one. It is OK to omit the semicolon
2828
between the last statement in a block and the closing C<}>.
2929
3030
{ say "Hello"; say "World" }
@@ -129,7 +129,8 @@ the expression it is contained in is evaluated:
129129
# This says "(..1 ..2 ..3)" not "(..1 ...2 ....3)"
130130
my $f = "."; say do { $f ~= "." } X~ 1, 2, 3;
131131
132-
In other words, it follows the same reification rules as everything else.
132+
In other words, it follows the same
133+
L<reification|/language/glossary#index-entry-Reify> rules as everything else.
133134
134135
Technically, C<do> is a loop which runs exactly one iteration.
135136
@@ -574,21 +575,36 @@ for $*IN.lines -> $line { .say }
574575
575576
Iteration variables are always lexical, so you don't need to use C<my> to give
576577
them the appropriate scope. Also, they are read-only aliases. If you need them
577-
to be writable, use C«<->» instead of C«->». If you want to modify them
578-
within the block, add C<is copy>, e.g., C<<for @foo -> $f is copy {...>>.
578+
to be writable, use C«<->» instead of C«->». Alternatively, you can add
579+
the L«C«is rw»|/type/Signature#index-entry-trait_is_rw» trait; this performs
580+
a binding operation so assigning to the parameter changes the value of the
581+
variable at the caller side. If instead you want to modify copies of the
582+
arguments within the block, add
583+
L«C«is copy»|/type/Signature#index-entry-trait_is_copy».
579584
580585
=begin code
581586
my @foo = 1..3;
582-
my @bar;
583-
584587
for @foo <-> $value {
585-
given $value {
586-
when $value %% 2 { $value = $value but "Even" }
587-
default { $value = $value but "Odd" }
588-
}
589-
@bar.push: $value;
588+
$value = $value %% 2 ?? "Even" !! "Odd"
589+
}
590+
591+
say @foo; # OUTPUT: «[Odd Even Odd]»
592+
593+
@foo = 1..3;
594+
for @foo -> $value is rw {
595+
$value = $value %% 2 ?? "Even" !! "Odd"
590596
}
591597
598+
say @foo; # OUTPUT: «[Odd Even Odd]»
599+
600+
@foo = 1..3;
601+
my @bar;
602+
for @foo -> $value is copy {
603+
$value = $value %% 2 ?? "Even" !! "Odd";
604+
@bar.push: $value
605+
}
606+
607+
say @foo; # OUTPUT: «[1 2 3]»
592608
say @bar; # OUTPUT: «[Odd Even Odd]»
593609
=end code
594610
@@ -634,7 +650,7 @@ for 1 {
634650
635651
The first loop creates three elements but they are in a sink context, so its
636652
C<sink> method is called. In the second loop, its last statement will be in a
637-
sink context, so it will be also sunk (from version 6.d).
653+
sink context, so it will be also sunk (from version 6.d).
638654
639655
The C<Empty> constant will act as a no-op for a loop:
640656
@@ -677,20 +693,30 @@ X<|lazy list,gather>X<|lazy list,take>
677693
678694
C<gather> is a statement or block prefix that returns a L<sequence|/type/Seq>
679695
of values. The values come from calls to L<take|/type/Mu#routine_take> in the
680-
dynamic scope of the C<gather> block.
681-
682-
my @a = gather {
683-
take 1;
684-
take 5;
685-
take 42;
696+
dynamic scope of the C<gather> block. In the following example, we implement
697+
a subroutine to compute the factors of an integer with C<gather> (note that the
698+
factors are not generated in a strictly increasing order):
699+
700+
sub factors( Int:D \n ) {
701+
my $k = 1;
702+
gather {
703+
while $k**2 < n {
704+
if n %% $k {
705+
take $k;
706+
take n div $k;
707+
}
708+
$k++;
709+
}
710+
take $k if $k**2 == n;
711+
}
686712
}
687-
say join ', ', @a; # OUTPUT: «1, 5, 42␤»
713+
714+
say factors(36); # OUTPUT: «1, 36, 2, 18, 3, 12, 4, 9, 6␤»
688715
689716
C<gather/take> can generate values lazily, depending on context. If you want to
690717
force lazy evaluation use the L<lazy|/type/Iterable#method_lazy> subroutine or
691718
method. Binding to a scalar or sigilless container will also force laziness.
692-
693-
For example
719+
For example:
694720
695721
my @vals = lazy gather {
696722
take 1;

0 commit comments

Comments
 (0)