In Operators§
See primary documentation in context for Hyper operators
Hyper operators include «
and »
, with their ASCII variants <<
and >>
. They apply a given operator enclosed (or preceded or followed, in the case of unary operators) by «
and/or »
to one or two lists, returning the resulting list, with the pointy part of «
or »
aimed at the shorter list. Single elements are turned to a list, so they can be used too. If one of the lists is shorter than the other, the operator will cycle over the shorter list until all elements of the longer list are processed.
say (1, 2, 3) »*» 2; # OUTPUT: «(2 4 6)»say (1, 2, 3, 4) »~» <a b>; # OUTPUT: «(1a 2b 3a 4b)»say (1, 2, 3) »+« (4, 5, 6); # OUTPUT: «(5 7 9)»say (, , )».(0.5);# OUTPUT: «(0.479425538604203 0.877582561890373 0.707106781186548)»
The last example illustrates how postcircumfix operators (in this case .()) can also be hypered.
my = <1 2 3>;my = <4 5 6>;say (,)»[1]; # OUTPUT: «(2 5)»
In this case, it's the postcircumfix[] which is being hypered.
Assignment metaoperators can be hyped.
my = 1, 2, 3;say »+=» 1; # OUTPUT: «[2 3 4]»my (, , );((, ), ) «=» ((1, 2), 3);say "$a, $c"; # OUTPUT: «1, 3»
Hyper forms of unary operators have the pointy bit aimed at the operator and the blunt end at the list to be operated on.
my = True, False, True;say !« ; # OUTPUT: «[False True False]»my = 1, 2, 3;»++;say ; # OUTPUT: «[2 3 4]»
Hyper operators are defined recursively on nested arrays.
say -« [[1, 2], 3]; # OUTPUT: «[[-1 -2] -3]»
Also, methods can be called in an out of order, concurrent fashion. The resulting list will be in order. Note that all hyper operators are candidates for parallelism and will cause tears if the methods have side effects. The optimizer has full reign over hyper operators, which is the reason that they cannot be defined by the user.
my CarefulClass ;my = ».take-care();my ; # May Contain Nuts».?this-method-may-not-exist();
Hyper operators can work with hashes. The pointy direction indicates if missing keys are to be ignored in the resulting hash. The enclosed operator operates on all values that have keys in both hashes.
%foo «+» %bar; | intersection of keys |
%foo »+« %bar; | union of keys |
%outer »+» %inner; | only keys of %inner that exist in %outer will occur in the result |
my = 1, 2, 3 Z=> <a b c>;my = 1, 2 Z=> <x z>;say «~» ; # OUTPUT: «{"1" => "ax", "2" => "bz"}»
Hyper operators can take user-defined operators as their operator argument.
sub pretty-file-size (Int --> Str)for 60, 50, 40, 30, 20, 10 -># OUTPUT: «10 EB 4 EB 2 PB 5 PB 0.5 PB 4 TB 300 GB 4.5 GB 50 MB 200 MB 9 KB 0.6 MB»
Whether hyperoperators descend into child lists depends on the nodality of the inner operator of a chain. For the hyper method call operator (».), the nodality of the target method is significant.
say (<a b>, <c d e>)».elems; # OUTPUT: «(2 3)»say (<a b>, <c d e>)».&; # OUTPUT: «((1 1) (1 1 1))»
You can chain hyper operators to destructure a List of Lists.
my = ((-1, 0), (0, -1), (0, 1), (1, 0));my = (2, 3);say »>>+<<» (, *); # OUTPUT: «((1 3) (2 2) (2 4) (3 3))»