Tags
There are no tags for this page.
Attachments
Perl 6
Amazing Perl 6
This page lists some amazing Perl 6 snippets. The list after the title refers to the keywords for the snippet.
Factorial
- custom operators
- reduce operator
- range operator
- placeholder variable
sub postfix:<!> { [*] 1..$^n }
say 5!;
Explanation
Custom Operators
There is a set of special sub declarations that interfere in the parsing of the language, these are the:
- prefix
- postfix
- circumfix
- infix
- postcircumfix
you declare them by using type:identifier, where the identifier is quoted using the '<' and '>', optionally « » when you want to use '<' or '>' as part of the identifier. Some examples of subs that come defined in Perl 6 core are:
- prefix:<^> which is the operator that turns ^100 into 0..99
- postfix:<i> which allows you to use complex numbers as in 1 + 2i
- circumfix:<( )> which creates a unflattened capture as in (1,2,(3,4,(5,6)))
- infix:<+> which sums two values, as in 1 + 2
- postcircumfix:<{ }> which is used as the hash subscript as in %a{$b}
So when you declare a sub postfix:<!> you are declaring a language operator that lives in the same level as any other Perl 6 operator.
Gather/Take
- gather/take
- lazy lists
- bind
my @a := gather for 1..1000 -> $i { say $i; take $i };
say @a[10]; # prints 1 2 3 4 5 6 7 8 9 10
|