Programmers Heaven jobs
Tools/Engine Programmer
Game Developers -
Sr Flash ActionScript Programmer


More jobs                Post a job


Perl 6 FAQ - Junctions

This FAQ is part of the Programmer's Heaven Perl 6 FAQ. It answers questions about Perl 6 junctions.

What is a junction?

Junctions are new in Perl 6. You can think of a junction as a variable that may have more than one value at the same time. Junctions are stored in scalar variables, and can be used anywhere that a single (scalar) value would be used.

A junction also indicates that there is a certain type of relationship between its values. There are four types of relationship that a junction can have: "all", "any", "one" or "none". They are best understood by looking at them individually.

What is the any (|) junction?

The "any" junction, constructed using the "any" function or the "|" operator, carries an "OR" relationship between all of its values. It can be used as follows:

if $answer eq 'yes' | 'no' {
    say "Woo, a valid answer!";
} else {
    say "Boo, an invalid answer!";
}


If $answer was one of yes or no, the condition would evaluate to true. Otherwise, it would evaluate to false. This is equivalent to:

if $answer eq 'yes' || $answer eq 'no' {
    say "Woo, a valid answer!";
} else {
    say "Boo, an invalid answer!";
}


It is also possible to use the "any" constructor:

if $answer eq any('yes', 'no') {
    say "Woo, a valid answer!";
} else {
    say "Boo, an invalid answer!";
}


You can use an array with the "any" constructor, which will create a junction whose possible values are those in the array.

my @valid = ('yes', 'no');
if $answer eq any(@valid) {
    say "Woo, a valid answer!";
} else {
    say "Boo, an invalid answer!";
}


The any junction is maybe the most useful of the four. Note that the junction can have as many values as you need at the same time:

if $dice_roll == 2 | 4 | 6 {
    say "You rolled an even, that means you aren't odd.";
}


What is the all (&) junction?

The "all" junction, constructed using the "all" function or the "&" operator, carries an "AND" relationship between all of its values. For example, if you wanted to check that every element of an array @a was zero, you could write:

if all(@a) == 0 {
    say "All zero";
}


What is the one (^) junction?

The "one" junction, constructed using the "one" function or the "^" operator, carries a kind of "exclusive OR" relationship between all of its values. If you use a "one" junction in a comparrison this is like asking, "does exactly one value in the junction match the condition?"

In the following example the first array contains only one zero, whereas the second contains two. The second "if" statement's condition evaluates to false since more than one of the junction's values matches the "equal to zero" condition.

my @a = (1,2,0,4,5);
my @b = (1,2,0,0,5);
if one(@a) == 0 {
    say "Only one zero in a";  # This prints
}
if one(@b) == 0 {
    say "Only one zero in b";  # This does not
}


What is the none junction?

The "none" junction is somewhat different from the others in that it does not have an operator to construct it; it only has the "none" function. It is useful when you want to check that a variable is anything but a certain selection of other values.

if $food eq none('octopus', 'marmite') {
    say "Tasty!";
}


The above program will print "Tasty!" unless $food is either octopus or marmite.

What happens if I perform an operation on a junction?

The junction automatically "threads through" the operation, resulting in another junction with all possible results. Here is an example. Note that the ".perl" method is used when printing a junction to turn it into a readable string form.

my $j = 1 | 2 | 3;
my $k = $j + 2;
say $k.perl;   # Outputs "any(3,4,5)"


The result of the operation is a new junction with the operation in question performed on each of the values that $j could be.

Operations involving two junctions are interesting.

my $i = 1 | 2;
my $j = 3 | 4;
my $k = $i + $j;
say $k.perl;  # Outputs "any(any(4,5),any(5,6))"


Notice that the junction that is returned (which is actually made up of nested junctions) is made up of all possible answers for adding the values that the junctions could be: 1 + 3, 1 + 4, 2 + 3 and 2 + 4. The nesting does not affect the behavior of the junction when it is used in a conditional:

my $j = 1 | 2;
my $k = 3 | 4;
if $j + $k == 5 {
    say "5 is a possible answer"; # this prints
}


What happens if I call a method on a junction?

The method will be called independently on each of the values in the junction and a junction containing all of the return values will be constructed.

my $location = 'Fuengirola' | 'Spain' | 'Europe';
say $location.chars.perl;  # Outputs "any(5,6,10)"


Note that the chars method gets the length of a string in characters. The result is a junction of all the possible string lengths. Note that a junction is a set, so duplicate values will only appear once:

my $location = 'Scarborough' | 'North Yorks' | 'England';
say $location.chars.perl;  # Outputs "any(7,11)"


What happens if I pass a junction as a parameter?

The subroutine will be called independently once for each of the values in the junction. That is, if a junction has five possible values then the subroutine will be called five times. The return values will be collected into a new junction.

To demonstrate this, try the following program:

sub inc($x is rw) {
    say "inc called";
    $x = $x + 1;
}
my $a = 1 | 2;
)
inc($a);
say $a.perl;


The output is:

inc called
inc called
any(2,3)


Note that "inc called" is printed twice - once for each value in the junction - even though only one call to "inc" is written in the program.


Back To FAQ List | Next Section

What's next?




Join our Perl 6 Newsletter
Email:



Visit our Perl Resources


  User Comments


riya

From India
(Report as abusive)
"Very Useful"
this faqs made to know about many things unknown and it helped me a lot
  View all   Rate and comment this article




 
Printer friendly version of the Perl6-FAQ-Junctions page


Sponsored links

Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Software Localization Tool Sisulizer
Localize DotNet, C++ Builder, Delphi, C/C++, Visual Basic & Java apps & html help. Try Sisulizer now
CSTSOFT Instrumentation .NET & ActiveX Components
A collection of 13 instrumentation .NET/ActiveX/VCL components including Gauge,Knob,LED,Trend etc.
Six Sigma Certification
100% Online-Six Sigma Certificate from Villanova - Find Out More Now.


Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.