More examples with Junctions


examples/junctions/operators.p6
#!/usr/bin/perl6
use v6;

my $options = 1|2|3;

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

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

examples/junctions/substr.p6
#!/usr/bin/perl6
use v6;

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

examples/junctions/j.p6
#!/usr/bin/perl6
use v6;

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' } # f    which is the same as
if !($x == 1) { say 't' } else { say 'f' } # f



#TODO
#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
#

examples/junctions/x.p6
#!/usr/bin/perl6
use v6;

#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;




examples/junctions/any.p6
#!/usr/bin/perl6
use v6;

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



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


Copyright 2006, 2007, 2008, 2009, 2010 Gabor Szabo http://szabgab.com/ Index | TOC
If you are interested in on-site trainings by the author, please contact me directly.

Online courses:

Would you like to get
updated when I publish
the next article?

Follow me:

Google Plus Twitter RSS feed