TomDLux has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking at the Perl6 code in rakudo-star, because I have some ideas about junctions, and I want to see how they correspond to what's actually being implemented.

Junction.pir has a lot of stuff, but it's fairly low-level, so it doesn't add up to all that much, just what you expect, new(), boolean comparisons, .perl .... Junction.pm is tiny and contains something interesting ... but challenging to decypher:

method postcircumfix:<( )>($c) { my @result = $.eigenstates.map({ $^code(|$c) }); Junction.new(@result, type => self!type); }

It defines parentheses that go after the junction object, wrapped around an argument that's assigned to the parameter $c, I think. The Junction 1 | 2 | 3 has one attribute $.type which in this case is any, and another attribute $.eigenstates containing the list (1,2,3). So the body of the method is map-ing across the list with the code block { $^code(|$c) }. $^code is an anonymous positional parameter ... the only one in the code block, and it uses the postcircumflex parameter $c as it's argument ... but what is $^code? What is a reasonable value for the $c? I tried:

my $k = $j2 ); my $k = $j( {^c != 2} ); # But both generated the message .. invoke() not implemented in class 'Integer'

My hope is that there is a way to alter a Junction object. In Sudoku, every cell begins with the potential to be any digit:  1|2|3|4|5|6|7|8|9. As cells are initialize to a specific value, all cells in the same row, the same column, and the same region (What's a quadrant when there are nine of them?) no longer have the potential to contain that value.</>

Alternately, the Conway-esque Quantum Superposition interpretation is that the cells ARE all those values at the same time: 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9. As cells are assigned values, the related cells lose the potential to contain that value ... one little Schrodinger's Digit dies, until each cells has a single specific value.

It would be equally useful for other problems which involve building up combinations or narrowing down possibilities: games like Battleships, Fish or Canasta, crossword puzzles, Hangman ... not necessarily the best solution for any of them, I'm saying, but a possibility.

I was hoping that that postcircumflex multi would enable me to do the necessary building up, or breaking of cyanide capsules, but at the moment, I'm confused

As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Replies are listed 'Best First'.
Re: Perl6 junctions and postcircumflex
by moritz (Cardinal) on Aug 24, 2010 at 08:36 UTC
    but what is $^code?

    It's a positional parameter of block that is passed to map.

    The block could also have been written as { $_(|$c) } or -> $code { $code(|$c) }

    What is a reasonable value for the $c?

    To clarify, this method is called when you try to invoke a Junction like a subroutine or codeblock. Which of course only makes sense if the Junction contains callable eigenstates. $c holds the parameter list that was passsed to this call. IT can be empty, if you like that:

    $ perl6 -e 'any({say 42}, {say 23}).()' 42 23 # with arguments: $ perl6 -e 'any({say $^x }, { say 40 + $^x }).(2)' 2 42
    My hope is that there is a way to alter a Junction object. In Sudoku, every cell begins with the potential to be any digit

    You are trying to abuse junctions as sets, which is explicitly discouraged.

    I know the excitement when learning about junctions, but after experimenting a bit them I'm firmly convinced that using them for everything but simple matchers is a broad over-use, and makes your program hard to write, read and understand.

    Perl 6 - links to (nearly) everything that is Perl 6.