Skip to content

Commit

Permalink
Merge pull request #378 from pdl/pr-index-more-operators
Browse files Browse the repository at this point in the history
Index more operators
  • Loading branch information
zoffixznet committed Feb 3, 2016
2 parents 7f4a641 + 5952ee5 commit bdf35b1
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 19 deletions.
38 changes: 20 additions & 18 deletions doc/Language/operators.pod
Expand Up @@ -188,7 +188,7 @@ the blunt end at the list to be operated on.
my @wisdom = True, False, True;
say !« @wisdom; # [False True False]
my @a = 1,2,3;
@a»++; # (2,3,4)
Expand Down Expand Up @@ -220,8 +220,8 @@ C<%foo »+« %bar;> union of keys
C<%outer »+» %inner;> only keys of %inner that exist in %outer will occur in the result
=end table
my %outer = 1,2,3 Z=> <a b c>;
my %inner = 1,2 Z=> <x z>;
my %outer = 1,2,3 Z=> <a b c>;
my %inner = 1,2 Z=> <x z>;
say %outer «~» %inner; # {"1" => "ax", "2" => "bz"}
Hyper operators can take user defined operators as its operator argument.
Expand All @@ -231,7 +231,7 @@ Hyper operators can take user defined operators as its operator argument.
sub infix:<r/>(Int \i1, Int \i2) {
round(i1 / i2, 0.1)
}
# we build a vector of fractions of $size and zip that with the fitting prefix
for $size «[r/]« (2**60, 2**50, 2**40, 2**30, 2**20, 2**10)
Z <EB PB TB GB MB KB> -> [\v,\suffix] {
Expand All @@ -241,7 +241,7 @@ Hyper operators can take user defined operators as its operator argument.
# this be smaller or equal then 0.4 KB
return $size.Str;
}
for 60, 50, 40, 30, 20, 10 -> $test {
my &a = { (2 ** $test) * (1/4, 1/2, 1, 10, 100).pick * (1..10).pick };
print pretty-file-site(a.Int) xx 2, ' ';
Expand All @@ -253,7 +253,7 @@ Hyper operators do not descent into child lists. You can chain hyper operators
to destructure a List of Lists.
my $neighbors = ((-1, 0), (0, -1), (0, 1), (1, 0));
my $p = (2,3);
my $p = (2,3);
say $neighbors »>>+<<» ($p, *); # ((1 3) (2 2) (2 4) (3 3))
=head2 Reduction Operators
Expand All @@ -263,12 +263,12 @@ element by element and return the resulting value.
say [+] 1,2,3; # 6
They can be defined as a list prefix operators or will be generated automatically.
They can be defined as a list prefix operators or will be generated automatically.
multi infix:<[~~]> (@c, &test) is looser(&infix:<~~>) {
gather for @c { take $_ if test($_) }
};
my @l = 1,'a',2,'b';
multi infix:<[~~]> (@c, &test) is looser(&infix:<~~>) {
gather for @c { take $_ if test($_) }
};
my @l = 1,'a',2,'b';
say @l [~~] {$^a ~~ Str}; # (a b)
For list infix operators, flattening is not done on the input list. This
Expand All @@ -278,7 +278,7 @@ allows list operators to become the reduction operator.
By default reduction meta operators are eager. To lazily generate values,
prefix the operator with a C<\>. If the non-meta part contains a C<\> already,
quote it with C<[]> (e.g. C<[\[\x]]>).
quote it with C<[]> (e.g. C<[\[\x]]>).
my $lazy := [\+] 1..*;
say $lazy[^10]; # (1 3 6 10 15 21 28 36 45 55)
Expand All @@ -298,12 +298,12 @@ The zip metaoperator, C<Z>, will apply a given infix operator to pairs taken
one left, one right, from its arguments. The resulting list is returned.
my @l = <a b c> Z~ 1,2,3; # [a1 b2 c3]
If one of the operands runs out of elements prematurely, the zip operator will
stop. An infinite list can be used to repeat elements. A list with a final
stop. An infinite list can be used to repeat elements. A list with a final
element of C<*> will repeat its 2nd last element indefinitely.
my @l = <a b c d> Z~ ':' xx *; # <a: b: c: d:>
my @l = <a b c d> Z~ ':' xx *; # <a: b: c: d:>
my @l = <a b c d> Z~ 1, 2, *; # <a1 b2 c2 d2>
=head2 Sequential Operators
Expand All @@ -318,9 +318,9 @@ done by the optimizer. Most simple infix operators are supported.
To avoid ambiguity when chaining meta operators use square brackets to help the
compiler to understand you.
my @a = 1,2,3;
my @b = 5,6,7;
@a X[+=] @b;
my @a = 1,2,3;
my @b = 5,6,7;
@a X[+=] @b;
say @a; # [19 20 21]
=head1 Z<>Term Precedence
Expand Down Expand Up @@ -537,6 +537,7 @@ method call.
1.i # No such method 'i' for invocant of type 'Int'
Technically this is not an operator, but syntax special-cased in the compiler.
X<|postfix call>
=head2 postfix C«.:<prefix>»
Expand All @@ -547,6 +548,7 @@ A prefix can be called like a method using colonpair notation. For example:
say $a.:<++>; # 3
Technically this is not an operator, but syntax special-cased in the compiler.
X<|prefix call>
=head2 postfix C«.::»
Expand Down

0 comments on commit bdf35b1

Please sign in to comment.