diff --git a/doc/Language/control.rakudoc b/doc/Language/control.rakudoc index a857304e7..8fafadcd6 100644 --- a/doc/Language/control.rakudoc +++ b/doc/Language/control.rakudoc @@ -1210,6 +1210,22 @@ for @x -> $x { prints "1245". +You can also use C in a C: the above example then looks +like: + +=begin code +my @x = 1, 2, 3, 4, 5; +print @x.map: -> $x { + next if $x == 3; + $x +} +=end code + +prints "1 2 4 5" because a space is added between entries of a C +when it is stringified. Note that that C was not put inside +the block of the C, as it generally considered bad practice to +run a C for its side-effects (in this case, the C. + If the L phaser|/language/phasers#NEXT> is present, it runs before the next iteration: @@ -1229,6 +1245,20 @@ while ($i < 10) { # OUTPUT: «1 is odd.␤3 is odd.␤5 is odd.␤7 is odd.␤9 is odd.␤» =end code +In version 6.e.PREVIEW (available as of the 2021.07 Rakudo compiler +release), it is also possible to return a value with the C +statement. This is particularly useful when using it in a C: + +=begin code +my @x = 1, 2, 3, 4, 5; +print @x.map: -> $x { + next 42 if $x == 3; + $x +} +=end code + +prints "1 2 42 4 5". + In a L block, C immediately exits the block for the current value: @@ -1250,7 +1280,6 @@ last statement values returns C for the iterations they run on.* The C command immediately exits the loop in question. =begin code - my @x = 1, 2, 3, 4, 5; for @x -> $x { last if $x == 3; @@ -1260,6 +1289,22 @@ for @x -> $x { prints "12". +You can also use C in a C: the above example then looks +like: + +=begin code +my @x = 1, 2, 3, 4, 5; +print @x.map: -> $x { + last if $x == 3; + $x +} +=end code + +prints "1 2" because a space is added between entries of a C when +it is stringified. Note that that C was not put inside the +block of the C, as it generally considered bad practice to run +a C for its side-effects (in this case, the C. + If the L phaser|/language/phasers#LAST> is present, it runs before exiting the loop: @@ -1280,8 +1325,22 @@ while ($i < 10) { # OUTPUT: «The last number was 5.␤» =end code -*Since version 6.d, the C command in a loop that collects its last -statement values returns C for the iterations they run on.* +Since version 6.d, the C command in a loop that collects its last +statement values returns C for the iterations they run on. + +In version 6.e.PREVIEW (available as of the 2021.07 Rakudo compiler +release), it is also possible to return a value with the C +statement. This is particularly useful when using it in a C: + +=begin code +my @x = 1, 2, 3, 4, 5; +print @x.map: -> $x { + last 42 if $x == 3; + $x +} +=end code + +print "1 2 42". =head1 X