Statement prefixes
Prefixes that alter the behavior of a statement or a set of them
Statement prefixes are written in front of a statement, and change their meaning, their output, or the moment they are going to be run. Since they have a specific behavior, they are also sometimes specific to some statement or group of statements.
lazy
As a statement prefix, lazy
acts in front of any statement, including for
loops, saving the execution for when the variable they are assigned to is actually needed.
my = 0;my = lazy for <1 2 3 4> ->;say ; # OUTPUT: «0»say eager ; # OUTPUT: «(0 1 2 3)»say ; # OUTPUT: «4»
The $incremented
variable is only incremented, that is, the internal part of the loop is only run when we eagerly evaluate the variable $var
that contains the lazy loop. Eagerness can be applied on a variable in other ways, such as calling the .eager
method on it.
my = lazy ;say ; # OUTPUT: «[...]»say .eager; # OUTPUT: «[0 1 4]»
This prefix can also be used in front of gather
to make the inner statements behave lazily; in general, any set of statements that returns a value will be made lazy using this.
eager
The eager
statement prefix will eagerly return the result of the statements behind, throwing away laziness and returning the result.
my := eager gather ;say [0]; # OUTPUT: «HeyHeyHey1»
gather
is implicitly lazy when bound to a scalar. However, with eager
as a statement prefix it will run all three iterations in the loop, as shown by the printed "Hey", even if we are just requesting the first one in a row.
hyper
, race
hyper
and race
use (maybe simultaneous) threads to run different iterations in a loop:
my = hyper for ^100_000
This code is around 3x faster than the bare for. But there are a couple of caveats here:
The operation inside the loop should take enough time for threading to make sense.
There should be no read or write access to the same data structure inside the loop. Let the loop produce a result, and assign it.
If there's an I/O operation inside the loop, there might be some contention so please avoid it.
Main difference between hyper
and race
is the ordering of results. Use hyper
if you need the loop results to be produced in order, race
if you don't care.
quietly
As a statement prefix, quietly
suppresses all warnings produced by the statement it precedes.
sub marine() ;quietly say ~; # OUTPUT: «marine»
Calling .Str
on code
produces a warning. Preceding the statement with quietly
will just produce the output, the name of the routine.
try
If you use try
in front of a statement, it will contain the exception produced in it and store it in the $!
variable, just like when it's used in front of a block.
try [].pop;say $!; # OUTPUT: «Cannot pop from an empty Array..»
do
do
can be used as an statement prefix to disambiguate the statement they precede; this is needed, for instance, if you want to assign the result of a for
statement. A bare for
will fail, but this will work:
my = 0;my = do for ^5 ;say ; # OUTPUT: «5»say ; # OUTPUT: «(0 1 2 3 4)»
do
is equivalent, as in other cases, to surrounding a statement with a parenthesis. It can be used as an alternative with a (possibly more) straightforward syntax.
sink
As in the case of the routine, sink
will run the statement throwing away the result. Use it in case you want to run some statement for the side effects it produces.
my = 0;my = sink for ^5 ;say ; # OUTPUT: «5»say ; # OUTPUT: «(Any)»
once
Within a loop, runs the prefixed statement only once.
my ;my = do for ^5 ;say ; # OUTPUT: «(0 1 2 3 4)»
gather
gather
can be used in front of a statement, receiving and gathering in a list all data structures emitted from a take
run anywhere from that statement:
proto sub fact( Int )multi sub fact( 1 --> 1 )multi sub fact( )my = gather say fact(13); # OUTPUT: «6227020800»say ;# OUTPUT: «[2 6 24 120 720 5040 40320 362880 3628800 ...]»
In this example, gather
precedes say
, which prints the first result of the factorial; at the same time, it's harvesting the result from every call to fact
, which goes to @factor
.
start
As a statement prefix, start
behaves in the same way as in front of a block, that is, it runs the statement asynchronously, and returns a promise.
proto sub fact( Int )multi sub fact( 1 --> 1 )multi sub fact( )my = gathersay await ;
The Promise
s created by start are gathered in an array, which returns the result of the operation once the promises have been fulfilled.
react
react
can be used in concurrent programs to create blocks of code that run whenever some event occurs. It works with blocks, and also as a statement prefix.
my Channel .= new;for ^100my = ( start react whenever ->) for ^10;startawait ;
In this case react
prefixes whenever
, which makes a long sum with every number read from a channel.
supply
The keyword supply
creates on-demand supplies that you can tap. It pairs with emit
, which can be used anywhere from within a supply
prefixed statement.
my = ->my = supply cards;.tap( -> );.tap( -> , done => );# OUTPUT:# [...]# Drawing: 1♥# Drawing: 7♥# Drawing: 9♥# No more cards
In this example, supply
acts as prefix of the previously defined cards
routine. It would very well be defined as a block, but giving it a name in this case might increase legibility or simply give the responsibility of defining it to other module.