7.2. More examples with Junctions

Example 7-2. examples/junctions/operators.p6

#!/usr/bin/perl6

my $options = 1|2|3;

$options *= 2;    # now it has 2|4|6

for 1..8 -> $i {
    if ($i == $options) {
        say $i;
    }
}

Example 7-3. examples/junctions/substr.p6

#!/usr/bin/perl6

my $x = 0|6;
my $str = "Hello World";
my $sub = substr($str, $x, 5);

my @values = $sub.values;
say "{@values}";        # Hello World
say $sub.values[0];     # Hello
say $sub.values.elems;  # 2 - the number of elements in the junction

Example 7-4. examples/junctions/j.p6

#!/usr/bin/perl6

my $x = 1|2;                              # junction
if ($x == 1) { say 't' } else { say 'f' } # t
if ($x == 2) { say 't' } else { say 'f' } # t
if ($x == 3) { say 't' } else { say 'f' } # f 
if ($x != 1) { say 't' } else { say 'f' } # t 


my @values = $x.values;          # fetch the values of a junction, order is random
say $x.values.elems;             # 2  
say $x.values[0] + $x.values[1]; # 3

my $y = 1|1|2;
say $y.values.elems;             # 2
say $y.values[0] + $y.values[1]; # 3
# 1|1|2    is the same as 1|2

my $r1 = (1|2 == 1);  # (Bool::False | Bool::True)
my $r2 = (1|2 == 2);  # (Bool::False | Bool::True)

my $r3 = (1|2 == 3);  # (Bool::False)
                    # there is only one element because multiple same booleans are irrelevant
                    # this is the same reduction we saw with 1|1|2


Example 7-5. examples/junctions/x.p6

#!/usr/bin/perl6

#my $x = any(1,2,3);
#my $z = any(1);
#say $x - $z;

my @numbers = (1, 2, 3);
my @new = (5, 3);
if (all(@new) > all(@numbers)) {
    say "all bigger";
}

if (any(@new) > all(@numbers)) {
    say "there is at least one bigger";
}


#my $diff = all(@numbers) and none(@new);
#say $diff.perl;




Example 7-6. examples/junctions/any.p6

#!/usr/bin/perl6

my @names = <Foo Bar Moo>;
print "Please type in your name: ";
my $username = readline;
if $username eq any(@names) {
    say "Welcome $username";
} else {
    say "Unknown user $username";
}


 #<<
if ($username eq any(@names)) {
    say "Welcome $username";
}
>>