Chapter 14. Junctions and Sets
Junctions
A Junction is a combination of values that is mostly indistinguishable from
a single value. They have their roots in the math of quantum mechanics.
You may have heard of Schrödinger’s cat, who is both dead and alive at the same time—an analogy that
physicist used to show how ridiculous this all is. Well, the joke was on
him.
any
The first Junction is the any. This “any” is
lowercase and is not related to the type Any. It creates a value that can act
like, well, any of the ones you gave it:
my $first-junction = any( 1, 3, 7 );
You can make a Junction from an Array or any other Positional:
my $junction = any( @array ); # Array my $junction = any( 1 .. 10 ); # Range my $junction = any( 1 ... 10 ); # Sequence
Now you have a Junction of three values. It will only
ever have three values. You can’t take one away or add one. There’s no
interface to extract them or count them. You’re not supposed to know—or
even care—which values are in there. In fact, Junction is the
only builtin type that does not inherit from Any:
%perl6To exit type 'exit' or '^D' >my $first-junction = any( 1, 3, 7 );any(1, 3, 7) >$first-junction.^nameJunction >$first-junction.^mro((Junction) (Mu))
These are quite handy in complex conditions. Consider the annoying code you’ve had to write to test if a value is one of three possible numbers:
my $n = any( 1, 3, 7 );
if $n == 1 || $n == 3 || $n == 7 {
put "n is one of those values";
}Being clever with a Hash doesn’t actually feel that ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access