Prev: Reversed operators

Hyper Operators

Note! This site is about Perl 6.
If you are looking for a solution for Perl 5, please check out the Perl 5 tutorial.

Hyper operators are really interesting. They allow the extrapolation of a scalar operator to operate on a list of scalars.

The real operators are actually unicode characters(» and «) but using regular doubled angle-brackets also work.

Normally the arrows point inside, towards the operator and there are two lists on the two sides of the operator.

This will apply the regular infix operator to the pairs as taken from the two lists on the two sides and return a list with the same length.

If the list on one side is longer than on the other side perl throws an exception "Lists on either side of non-dwimmy hyperop of infix are not of the same length"

In order to make the operator dwimmy you need to turn the arrows around to point to the operand that should dwim. It can be either or both operands. If an operand is being pointed at (that is if it is supposed to dwim) then in case that operand is too short, perl will automatically use the values from the beginning to the end of it repeatedly as long as the other side needs pairs.

You can make both sides dwimmy if you don't know up front which one will be longer and if you want to make them work in both ways.

As a special case if one side is a single scalar and if the arrow points in its direction then that value will be paired with each one of the values from the array on the other side.


#!/usr/bin/env perl6
use v6;

my @x = (1, 2) >>+<< (3, 4);
say @x.perl;  # [4, 6]

#my @d = (1, 2) >>+<< (3);
#say @d.perl;
# Lists on either side of non-dwimmy hyperop of infix:<+> are not of the same length

my @z = (1, 2, 3, 4) >>+>> (1, 2);
say @z.perl;      # [2, 4, 4, 6]

@z = (1, 2, 3, 4) <<+>> (1, 2);
say @z.perl;      # [2, 4, 4, 6]

@z = (4) <<+>> (1, 2);
say @z.perl;      # [5, 6]

my @y = (1, 2) >>+>> 1;
say @y.perl;      # [2, 3]

There is a lot more to hyper operators but for now this should be enough.

tutorial/arrays/hyper.p6.out, 6), 4, 4, 6), 4, 4, 6), 6), 3)

The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Full name:
This is a newsletter temporarily running on my personal site ( using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2012-01-01

Prev: Reversed operators


In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus
Suggest a change
Elapsed time: 2.3853080

Support my Perl 6 book!

I am writing a book on Web Application Development in Perl 6 and raising money via crowdfunding. Please support my effort!

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.