Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this Raku program producing a Seq of Array rather than a simple Array?

my %f;
for $*HOME.dir() -> $file {
    my $filename = $file.basename;
    %f{$filename}.push: $file, rand;
}
my $p = %f.pick; # just need any old random element
say $p.^name;
say "{$p.values.^name} has {$p.values.elems} elements";
say "{$p.values[0].^name} has {$p.values[0].elems} elements";

say '';
say $*RAKU;
say $*DISTRO;
say $*KERNEL;
say $*VM;

Output:

Pair
Seq has 1 elements
Array has 2 elements

Raku (6.d)
macos (13.2.1)
darwin
moar (2023.02)

Why is the .values of $p a Seq of Array, rather than a simple Array?

like image 273
Jim Bollinger Avatar asked May 18 '26 12:05

Jim Bollinger


1 Answers

As your say $p.^name line shows, $p is a Pair. The value of this Pair is an Array; and if you called $p.value, you'd get that Array. But $p.values (with an "s") returns a sequence of all of $p's values – that is,Seq containing one Array.

Why is the .values of %f a Seq of Array

(I'm not sure this was a typo, but note that you called .values on the .first element of %f, not on %f itself. Calling .values on all of %f would also give you a Seq of Arrays, but with more elements (one for each entry in %f. Also note that %f (a Hash) isn't ordered, and thus .first doesn't return a consistent element – which is very rarely what you want.)

like image 77
codesections Avatar answered May 21 '26 01:05

codesections