How to return values โ€‹โ€‹in a connection as an array?

Define the connection my $j = 1 | 2 | 3 | 4 | 5 my $j = 1 | 2 | 3 | 4 | 5 my $j = 1 | 2 | 3 | 4 | 5 , now I want to get an array of its value [1 2 3 4 5] , how do I implement this?

I tried $j.values , but Perl6 gave me the whole connection as an element: [any((1), (2), (3), (4), (5))] .

+8
arrays perl6 junction
source share
2 answers

As Haakon Hegland has already pointed out, this is not what you should do:

Connections are intended to be used as helpers in a boolean context; transition introspection is not supported. If you feel the urge to explore the connection, use Set or the type associated with it instead.

- docs.perl6.org/type/Junction

However, this is possible.

First, you can use authothreading (i.e., automatically evaluate each branch branch when passing a function that expects an argument of type Any ):

 sub unjunc(Junction $j) { gather -> Any $_ { .take }.($j); } 

Secondly, you can push the guts out and manually extract the values:

 sub unjunc(Junction $j) { multi extract(Any $_) { .take } multi extract(Junction $_) { use nqp; my $list := nqp::getattr($_, Junction, '$!storage'); my int $elems = nqp::elems($list); loop (my int $i = 0; $i < $elems; $i = $i + 1) { extract nqp::atpos($list, $i); } } gather extract $j; } 

If your connection is not recursive (i.e. does not contain other connections that you want to smooth out), the latter approach can be simplified:

 my $j := 1|2|3; say nqp::p6bindattrinvres(nqp::create(List), List, '$!reified', nqp::getattr($j, Junction, '$!storage')); 
+6
source share

This, as I know, is intentional.

Suppose $ j contains a hash join: then $ j.values โ€‹โ€‹will be a Seq join, not the hashes themselves.

If you need a jump array, then perhaps you should start with an array and build a connection from this:

 my @a = 1,2,3,4,5; my $j = any(@a); 

If you really want to go the Junction โ†’ Array path, you can, but that would be to use nqp and something that I would not recommend in userland code.

+9
source share

All Articles