txt: ## statement-prefixes.md
  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`](#Statement_prefixes "go to top of document")[§](#lazy "direct link")
  
  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.
  
  ```raku
  my $incremented = 0;
  my $var = lazy for <1 2 3 4> -> $d {
      $incremented++
  };
  say $incremented; # OUTPUT: «0␤»
  say eager $var;   # OUTPUT: «(0 1 2 3)␤»
  say $incremented; # 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.
  
  ```raku
  my @array = lazy { (^3).map( *² )  };
  say @array;       # OUTPUT: «[...]␤»
  say @array.eager; # OUTPUT: «[0 1 4]␤»
  
  ```
  
  This prefix can also be used [in front of `gather`](/language/control#gather/take) to make the inner statements behave lazily; in general, any set of statements that returns a value will be made lazy using this.
  
  ## [`eager`](#Statement_prefixes "go to top of document")[§](#eager "direct link")
  
  The `eager` statement prefix will eagerly return the result of the statements behind, throwing away laziness and returning the result.
  
  ```raku
  my $result := eager gather { for 1..3 { say "Hey"; take $_² } };
  say $result[0]; # OUTPUT: «Hey␤Hey␤Hey␤1␤»
  
  ```
  
  `gather` is [implicitly lazy when bound to a scalar](/syntax/gather%20take). 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`](#Statement_prefixes "go to top of document")[§](#hyper,_race "direct link")
  
  A `for` loop will automatically serialize any [`HyperSeq`](/type/HyperSeq) or [`RaceSeq`](/type/RaceSeq) used in it; on the other hand `hyper` and `race` use (maybe simultaneous) threads to run different iterations in a loop:
  
  ```raku
  my @a = hyper for ^100_000 { .is-prime }
  
  ```
  
  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`](#Statement_prefixes "go to top of document")[§](#quietly "direct link")
  
  As a prefix, `quietly` suppresses all runtime warnings produced by the block or statement it precedes.
  
  ```raku
  sub marine() {};
  quietly say ~&marine; # OUTPUT: «marine␤»
  sub told-you { warn 'hey...' };
  quietly { told-you; warn 'kaput!' };
  warn 'Telling you now!';  # OUTPUT: «Telling you now!␤ [...] ␤»
  
  ```
  
  Calling [`.Str` on `code` produces a warning](/type/Code#method_Str). Preceding the code with `quietly` will just produce the output without warning.
  
  ## [`try`](#Statement_prefixes "go to top of document")[§](#try "direct link")
  
  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](/language/exceptions#try_blocks).
  
  ```raku
  try [].pop;
  say $!; # OUTPUT: «Cannot pop from an empty Array␤..»
  
  ```
  
  ## [`do`](#Statement_prefixes "go to top of document")[§](#do "direct link")
  
  `do` can be used as a 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:
  
  ```raku
  my $counter = 0;
  my $result = do for ^5 { $counter++ };
  say $counter; # OUTPUT: «5␤»
  say $result;  # 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`](#Statement_prefixes "go to top of document")[§](#sink "direct link")
  
  As in the [case of the routine](/routine/sink), `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.
  
  ```raku
  my $counter = 0;
  my $result = sink for ^5 { $counter++ };
  say $counter; #  OUTPUT: «5␤»
  say $result;  #  OUTPUT: «(Any)␤»
  
  ```
  
  The `sink` statement prefix will also convert [`Failure`](/type/Failure)s into exceptions:
  
  ```raku
  sub find-the-number ( Int $n where $n < 10 ) {
      if $n == 7 {
          return True;
      } else {
          fail "Not that number" ;
      }
  }
  for 1..^10 {
      try {
          sink find-the-number($_);
      };
      say "Found $_" unless $!;
  }
  
  ```
  
  In this case, we will know that the number has been found only when the `try` block is not catching an exception.
  
  ## [`react`](#Statement_prefixes "go to top of document")[§](#react "direct link")
  
  `react` can be used in concurrent programs to create blocks of code that run whenever some event occurs. It [works with blocks](/syntax/react), and also as a statement prefix.
  
  ```raku
  my Channel $KXGA .= new;
  for ^100 {
      $KXGA.send( (100000..200000).pick );
  }
  
  my @sums = ( start react whenever $KXGA -> $number {
      say "In thread ", $*THREAD.id;
      say "→ ", (^$number).sum;
  } ) for ^10;
  
  start { sleep 10; $KXGA.close(); }
  
  await @sums;
  
  ```
  
  In this case `react` prefixes `whenever`, which makes a long sum with every number read from a channel.
  