In Operators§
See primary documentation in context for infix ...
multi infix:<...>(**@) is assoc<list> multi infix:<...^>(**@) is assoc<list> multi infix:<^...>(**@) is assoc<list> multi infix:<^...^>(**@) is assoc<list>
The sequence operator, which can be written either as ...
or as …
, with variants ...^
, ^...
, ^...^
, …^
, ^…
and ^…^
, will produce (possibly lazy) generic sequences on demand. Such sequences are of the Seq
type.
The variants of the operator with an initial caret, ^...
, ^...^
, ^…
and ^…^
, produce sequences that do not contain the initial element.
The variants of the operator with a final caret, ...^
, ^...^
, …^
and ^…^
, produce sequences that do not contain the final element.
Note: the variants ^...
, ^...^
, ^…
and ^…^
have been available in Rakudo compiler starting from 2020.05 release.
The left-hand side of the operator specify the initial elements; it may include a generator after the first element or elements. The right-hand side will have an endpoint, which can be Inf
or *
for "infinite" lists (that is, lazy lists whose elements are only produced on demand), an expression which will end the sequence when True
, or other elements such as Junction
s.
The sequence operator invokes the generator with as many arguments as necessary. The arguments are taken from the initial elements and the already generated elements.
An endpoint of *
(Whatever
), Inf
or ∞
generates on demand an infinite sequence, with a default generator of *.succ
say (1 ... *)[^5]; # OUTPUT: «(1 2 3 4 5)»
Custom generators need to be the last element of the list before the '...' operator. This one takes two arguments, and generates the eight first Fibonacci numbers
say (1, 1, -> $a, $b { $a + $b } ... *)[^8]; # OUTPUT: «(1 1 2 3 5 8 13 21)» # same but shorter say (1, 1, * + * ... *)[^8]; # OUTPUT: «(1 1 2 3 5 8 13 21)»
The generator can also take only one argument.
say 5, { $_ * 2 } ... 40; # OUTPUT: «5 10 20 40»
Or it can use the anonymous state variable $
to skip one position in the sequence when computing the next one:
say (1, 1, 1, -> $a, $b, $ { $a + $b } … ∞)[3..10]; # OUTPUT: «(2 2 3 4 5 7 9 12)»
There must be at least as many initial elements as arguments to the generator.
If the endpoint is not *
, it's smartmatched against each generated element and the sequence is terminated when the smartmatch succeeded. The final element is excluded of the sequence if a sequence operator variant with a final caret is used, it is included otherwise.
This allows you to write
say 1, 1, * + * ...^ *>= 100; # OUTPUT: «(1 1 2 3 5 8 13 21 34 55 89)»
to generate all Fibonacci numbers up to but excluding 100.
The ...
operators consider the initial values as "generated elements" as well, so they are also checked against the endpoint:
my $end = 4; say 1, 2, 4, 8, 16 ... $end; # OUTPUT: «(1 2 4)»
If you do not provide a generator, the sequence operator tries to deduce the sequence. In most cases, this means using a default *.
succ or *.
pred, depending on how the end points compare:
say 1 ... 4; # OUTPUT: «(1 2 3 4)» say 4 ... 1; # OUTPUT: «(4 3 2 1)» say 1 ^... 4; # OUTPUT: «(2 3 4)» say 1 ...^ 4; # OUTPUT: «(1 2 3)» say 1 ^...^ 4; # OUTPUT: «(2 3)» say 'a' ... 'e'; # OUTPUT: «(a b c d e)» say 'e' ... 'a'; # OUTPUT: «(e d c b a)»
However, the sequence operator will deduce a different sequence in some special cases.
If you provide more than one initial element and all initial elements are numeric, the sequence operator tries find an arithmetic or geometric sequence that fits the pattern in the initial elements:
say 2, 4, 6 ... 12; # OUTPUT: «(2 4 6 8 10 12)» say 1, 2, 4 ... 32; # OUTPUT: «(1 2 4 8 16 32)» say 1, 2, 4 ^... 32; # OUTPUT: «(2 4 8 16 32)» say 1, 2, 4 ^...^ 32; # OUTPUT: «(2 4 8 16)»
If you provide one Str
initial element and a Str
final element with the same number of characters, then the sequence operator will deduce a sequence of all strings where each letter is le
the letter at the corresponding position in the final element:
say 'aa' … 'cc'; # OUTPUT: «(aa ab ac ba bb bc ca cb cc)» # Which is the same as say 'a'..'c' X~ 'a'..'c'; # OUTPUT: «(aa ab ac ba bb bc ca cb cc)»
In Operators§
See primary documentation in context for listop ...
Called the yada, yada, yada operator or stub operator, if it's the only statement in a routine or type, it marks that routine or type as a stub (which is significant in the context of pre-declaring types and composing roles).
If the ...
statement is executed, it calls fail, with the default message Stub code executed
.
This operator can be used for forward declarations of classes:
class Foo {...} class Bar { has Foo $.foo; } class Foo { say "This is a Foo object"; }
or routines, such as this Sub
:
sub a() { ... } say a; # OUTPUT: «42» sub a() { 42 }
Please note that, in this case, it's not really necessary, and it will work the same without that forward declaration.