Skip to Content
Learning Perl 6
book

Learning Perl 6

by brian d foy
September 2018
Beginner
474 pages
9h 35m
English
O'Reilly Media, Inc.
Content preview from Learning Perl 6

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:

% perl6
To exit type 'exit' or '^D'
> my $first-junction = any( 1, 3, 7 );
any(1, 3, 7)
> $first-junction.^name
Junction
> $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

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Think Perl 6

Think Perl 6

Laurent Rosenfeld, Allen B. Downey
Perl 6 Deep Dive

Perl 6 Deep Dive

Andrew Shitov
Perl Best Practices

Perl Best Practices

Damian Conway
Perl by Example

Perl by Example

Ellie Quigley

Publisher Resources

ISBN: 9781491977675Errata Page