Skip to content

Commit 47157fd

Browse files
authored
Merge pull request #2445 from uzluisf/master
Properly align comments and add few more examples
2 parents 1f2867f + cf7056f commit 47157fd

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

doc/Language/optut.pod6

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,63 @@
44
55
=SUBTITLE A short tutorial on how to declare operators and create new ones.
66
7-
Operators are declared by C<sub>
8-
followed by C<prefix>, C<infix>, C<postfix>, C<circumfix>, or C<postcircumfix>;
7+
Operators are declared by using the C<sub> keyword followed by
8+
C<prefix>, C<infix>, C<postfix>, C<circumfix>, or C<postcircumfix>;
99
then a colon and the operator name in a quote construct. For (post-)circumfix
1010
operators separate the two parts by white space.
1111
1212
sub hello {
1313
say "Hello, world!";
1414
}
15+
1516
say &hello.^name; # OUTPUT: «Sub␤»
1617
hello; # OUTPUT: «Hello, world!␤»
1718
1819
my $s = sub ($a, $b) { $a + $b };
1920
say $s.^name; # OUTPUT: «Sub␤»
2021
say $s(2, 5); # OUTPUT: «7␤»
2122
22-
sub postfix:<♥>($a){ say „I love $a!“ }
23-
42♥;
24-
# OUTPUT: «I love 42!␤»
25-
sub postcircumfix:<⸨ ⸩>(Positional $a, Whatever){ say $a[0], '…', $a[*-1] }
26-
[1,2,3,4]⸨*⸩;
27-
# OUTPUT: «1…4␤»
23+
# Alternatively we could create a more
24+
# general operator to sum n numbers
25+
sub prefix:<Σ>( *@number-list ) {
26+
[+] @number-list
27+
}
28+
29+
say Σ (13, 16, 1); # OUTPUT: «30␤»
30+
31+
sub infix:<:=:>( $a is rw, $b is rw ) {
32+
($a, $b) = ($b, $a)
33+
}
34+
35+
my ($num, $letter) = ('A', 3);
36+
say $num; # OUTPUT: «A␤»
37+
say $letter; # OUTPUT: «3␤»
38+
39+
# Swap two variables' values
40+
$num :=: $letter;
41+
42+
say $num; # OUTPUT: «3␤»
43+
say $letter; # OUTPUT: «A␤»
44+
45+
sub postfix:<!>( Int $num where * >= 0 ) { [*] 1..$num }
46+
say 0!; # OUTPUT: «1␤»
47+
say 5!; # OUTPUT: «120␤»
48+
49+
sub postfix:<♥>( $a ) { say „I love $a!“ }
50+
42♥; # OUTPUT: «I love 42!␤»
51+
52+
sub postcircumfix:<⸨ ⸩>( Positional $a, Whatever ) {
53+
say $a[0], '…', $a[*-1]
54+
}
55+
56+
[1,2,3,4]⸨*⸩; # OUTPUT: «1…4␤»
57+
2858
constant term:<♥> = "♥"; # We don't want to quote "love", do we?
29-
sub circumfix:<α ω>($a){ say „$a is the beginning and the end.“ };
30-
α♥ω;
31-
# OUTPUT: «♥ is the beginning and the end.␤»
59+
sub circumfix:<α ω>( $a ) {
60+
say „$a is the beginning and the end.“
61+
};
62+
63+
α♥ω; # OUTPUT: «♥ is the beginning and the end.␤»
3264
3365
=end pod
3466

0 commit comments

Comments
 (0)