Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What uses can you think of for Perl 6's junctions?

Tags:

perl

More information from the Perl 6 Wikipedia entry

Junctions

Perl 6 introduces the concept of junctions: values that are composites of other values.[24] In the earliest days of Perl 6's design, these were called "superpositions", by analogy to the concept in quantum physics of quantum superpositions — waveforms that can simultaneously occupy several states until observation "collapses" them. A Perl 5 module released in 2000 by Damian Conway called Quantum::Superpositions[25] provided an initial proof of concept. While at first, such superpositional values seemed like merely a programmatic curiosity, over time their utility and intuitiveness became widely recognized, and junctions now occupy a central place in Perl 6's design.

In their simplest form, junctions are created by combining a set of values with junctive operators:

my $any_even_digit = 0|2|4|6|8; # any(0, 2, 4, 6, 8)
my $all_odd_digits = 1&3&5&7&9; # all(1, 3, 5, 7, 9)

| indicates a value which is equal to either its left or right-hand arguments. & indicates a value which is equal to both its left and right-hand arguments. These values can be used in any code that would use a normal value. Operations performed on a junction act on all members of the junction equally, and combine according to the junctive operator. So, ("apple"|"banana") ~ "s" would yield "apples"|"bananas". In comparisons, junctions return a single true or false result for the comparison. "any" junctions return true if the comparison is true for any one of the elements of the junction. "all" junctions return true if the comparison is true for all of the elements of the junction.

Junctions can also be used to more richly augment the type system by introducing a style of generic programming that is constrained to junctions of types:

sub get_tint ( RGB_Color|CMYK_Color $color, num $opacity) { ... }
sub store_record (Record&Storable $rec) { ... }
like image 235
mbac32768 Avatar asked Sep 19 '08 14:09

mbac32768


3 Answers

How many days are in a given month?

given( $month ){
  when any(qw'1 3 5 7 8 10 12') {
    $day = 31
  }
  when any(qw'4 6 9 11') {
    $day = 30
  }
  when 2 {
    $day = 29
  }
}
like image 195
Brad Gilbert Avatar answered Oct 25 '22 21:10

Brad Gilbert


The most attractive feature of junctions is that you don't need to write a lot of code test for complex situations. You describe the situation with the junctions, then apply the test. You don't think about how you get the answer (for instance, using short circuit operators or if blocks) but what question you are asking.

like image 36
brian d foy Avatar answered Oct 25 '22 20:10

brian d foy


Autothreading sounds cool, although I don't know what its current status is.

for all(@files) -> $file {
    do_something($file);
}

Junctions have no order, so the VM is free to spawn a thread for every element in @files and process them all in parallel.

like image 25
Eevee Avatar answered Oct 25 '22 21:10

Eevee