|
| 1 | +=begin pod :tag<perl6> |
| 2 | +
|
| 3 | +=TITLE Statement prefixes |
| 4 | +
|
| 5 | +=SUBTITLE Prefixes that alter the behavior of a statement of set of them |
| 6 | +
|
| 7 | +Statement prefixes are not statements per se. They are written in front of a |
| 8 | +statement, and change their meaning, their output, or the moment they are going |
| 9 | +to be run. Since they have a specific behavior, they are also sometimes specific |
| 10 | +to some statement or statements. |
| 11 | +
|
| 12 | +=head2 X<C<lazy>|lazy (statement prefix)> |
| 13 | +
|
| 14 | +As a statement prefix, lazy acts on C<for> loops, saving the execution for when |
| 15 | +the variable they are assigned for is actually needed. |
| 16 | +
|
| 17 | +=for code |
| 18 | +my $incremented = 0; |
| 19 | +my $var = lazy for <1 2 3 4> -> $d { |
| 20 | + $incremented++ |
| 21 | +}; |
| 22 | +say $incremented; # OUTPUT: «0» |
| 23 | +say eager $var; # OUTPUT: «(0 1 2 3)» |
| 24 | +say $incremented; # OUTPUT: «4» |
| 25 | +
|
| 26 | +The C<$incremented> variable is only incremented, that is, the internal part of |
| 27 | +the loop is only run, when we eagerly evaluate the variable C<$var> that |
| 28 | +contains the lazy loop. Eagerness can be applied on a variable in other ways, |
| 29 | +such as calling the C<.eager> method on it. |
| 30 | +
|
| 31 | +This prefix can also be used |
| 32 | +L<in front of C<gather>|/language/control#gather/take> to make the inner |
| 33 | +statements behave lazily. |
| 34 | +
|
| 35 | +
|
| 36 | +
|
| 37 | +=end pod |
| 38 | + |
| 39 | +# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6 |
0 commit comments