Skip to content

Commit d166589

Browse files
committed
Creates a new file for statement prefixes.
This is for #534, but mainly for #2034 Still way to go.
1 parent 4d91061 commit d166589

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

doc/Language/00-POD6-CONTROL

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Quoting constructs FILE: quoting
8080
Regexes FILE: regexes
8181
Routines FILE: routines
8282
Sets, bags, and mixes FILE: setbagmix
83+
Statement prefixes FILE: statement-prefixes
8384
Subscripts FILE: subscripts
8485
Syntax FILE: syntax
8586
System interaction FILE: system

doc/Language/statement-prefixes.pod6

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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

Comments
 (0)