Yet Another Perl 6 Operator: Junction Operators


Maintainer: Adriano Ferreira <>
Date: 07 Jan 2008
Last Modified: 07 Jan Mon 2008
Number: 19
Version: 1
Status: Draft


Junctions and the repurposed |, & and ^ operators.


Perl 6 introduces a new scalar data-type: the "junction". A junction is a single scalar value that can act like two or more values at once.

example                 a value which acts like

any(1,2,3)              1 or 2 or 3
all(@vals)              all members of @vals at the same time
one(<moe curly larry>)  one of the three stooges
none(@bad_guys)         none of the listed bad guys

The operators '|', '&' and '^' are now junction constructors, providing a syntactical complement to the functional variants any, all, one and none.

$a  | $b                 any($a, $b)
$x  & $y                 all($x, $y)
$me ^ $you               one($me, $you)

The particular feature that make junctions interesting is that they thread through operations, returning another junction representing the result:

(1|2|3) + 4;            # 5|6|7
(1|2) + (3&4);          # (4|5) & (5|6)

The last example illustrates how when two junctions are applied through an operator, the result is a junction representing the operator applied to each combination of values.

Also operations on junctions will short-circuit as soon as possible, which will make them effective in both syntax and performance terms. For instance,

if $dave == 1|4|9 {
   say "I'm sorry, Dave, you're just a square.";

reads better (and probably more understandable/maintainable) than using

$dave == 1 || $dave == 4 || $dave == 9
# or
grep { $_ == $dave }, 1, 4, 9

and resumes as soon as the comparison is true. This is even more compelling when testing collections against other collections. Thus,

if all(@newvals) > any(@oldvals) {
  say "These are all bigger than something already seens."

is much finer than an equivalent version with nested greps (which is very prone to error when first writing — at least for mere human programmers).

Junctions are specifically unordered. So if you say

foo() | bar() | baz() == 42

the functions calls may happen in any order or in parallel. They can short-circuit as soon as any of them return 42. That makes junctions as well as other Perl 6 features like hyper-operators purposely amenable to parallelization.

Note. Consequently, with this choice of semantics for '|', '&' and '^', bitwise operators in Perl 6 now have different spellings (eg. '+|', '~|', etc.) – which is material for another micro-article.

See Also

"The Wonderful World of Junctions" from Exegesis 06

"Junctive operators" from Synopsis 03

$Revision: 144 $


Perl 6
junction operators
junctive operators