@@ -24,7 +24,7 @@ also be written as:
24
24
= head1 X < Blocks|control flow, blocks >
25
25
26
26
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
28
28
between the last statement in a block and the closing C < } > .
29
29
30
30
{ say "Hello"; say "World" }
@@ -129,7 +129,8 @@ the expression it is contained in is evaluated:
129
129
# This says "(..1 ..2 ..3)" not "(..1 ...2 ....3)"
130
130
my $f = "."; say do { $f ~= "." } X~ 1, 2, 3;
131
131
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.
133
134
134
135
Technically, C < do > is a loop which runs exactly one iteration.
135
136
@@ -574,21 +575,36 @@ for $*IN.lines -> $line { .say }
574
575
575
576
Iteration variables are always lexical, so you don't need to use C < my > to give
576
577
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» .
579
584
580
585
= begin code
581
586
my @foo = 1..3;
582
- my @bar;
583
-
584
587
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"
590
596
}
591
597
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]»
592
608
say @bar; # OUTPUT: «[Odd Even Odd]»
593
609
= end code
594
610
@@ -634,7 +650,7 @@ for 1 {
634
650
635
651
The first loop creates three elements but they are in a sink context, so its
636
652
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).
638
654
639
655
The C < Empty > constant will act as a no-op for a loop:
640
656
@@ -677,20 +693,30 @@ X<|lazy list,gather>X<|lazy list,take>
677
693
678
694
C < gather > is a statement or block prefix that returns a L < sequence|/type/Seq >
679
695
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
+ }
686
712
}
687
- say join ', ', @a; # OUTPUT: «1, 5, 42»
713
+
714
+ say factors(36); # OUTPUT: «1, 36, 2, 18, 3, 12, 4, 9, 6»
688
715
689
716
C < gather/take > can generate values lazily, depending on context. If you want to
690
717
force lazy evaluation use the L < lazy|/type/Iterable#method_lazy > subroutine or
691
718
method. Binding to a scalar or sigilless container will also force laziness.
692
-
693
- For example
719
+ For example:
694
720
695
721
my @vals = lazy gather {
696
722
take 1;
0 commit comments