Skip to content

Commit

Permalink
Document next/last with a value in 6.e
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Mar 7, 2023
1 parent e2eb0f2 commit 9f10710
Showing 1 changed file with 62 additions and 3 deletions.
65 changes: 62 additions & 3 deletions doc/Language/control.rakudoc
Expand Up @@ -1210,6 +1210,22 @@ for @x -> $x {

prints "1245".

You can also use C<last> in a C<map>: 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<Seq>
when it is stringified. Note that that C<print> was not put inside
the block of the C<map>, as it generally considered bad practice to
run a C<map> for its side-effects (in this case, the C<print>.

If the L<C<NEXT> phaser|/language/phasers#NEXT> is present, it runs
before the next iteration:

Expand All @@ -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<next>
statement. This is particularly useful when using it in a C<map>:

=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<whenever|/language/concurrency#index-entry-whenever>
block, C<next> immediately exits the block for the current value:

Expand All @@ -1250,7 +1280,6 @@ last statement values returns C<Empty> for the iterations they run on.*
The C<last> command immediately exits the loop in question.

=begin code

my @x = 1, 2, 3, 4, 5;
for @x -> $x {
last if $x == 3;
Expand All @@ -1260,6 +1289,22 @@ for @x -> $x {

prints "12".

You can also use C<last> in a C<map>: 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<Seq> when
it is stringified. Note that that C<print> was not put inside the
block of the C<map>, as it generally considered bad practice to run
a C<map> for its side-effects (in this case, the C<print>.

If the L<C<LAST> phaser|/language/phasers#LAST> is present, it runs
before exiting the loop:

Expand All @@ -1280,8 +1325,22 @@ while ($i < 10) {
# OUTPUT: «The last number was 5.␤»
=end code

*Since version 6.d, the C<last> command in a loop that collects its last
statement values returns C<Empty> for the iterations they run on.*
Since version 6.d, the C<last> command in a loop that collects its last
statement values returns C<Empty> 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<last>
statement. This is particularly useful when using it in a C<map>:

=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<redo|Control flow,redo>

Expand Down

0 comments on commit 9f10710

Please sign in to comment.