develooper Front page | perl.perl6.language | Postings from May 2020

Fwd: unflattering flat

From:
William Michels via perl6-language
Date:
May 2, 2020 07:26
Subject:
Fwd: unflattering flat
Message ID:
CAA99HCyEcAh+ktcWD-Xq9Brr_Cmn8G-akkhCaswm4AD2Q90HMg@mail.gmail.com
Migrating this question over from perl6-users <perl6-users@perl.org> :

Can someone explain why in the second and third REPL code lines below,
line 2 returns a List while line 3 returns a Seq? And is there a
general rule to remember which code returns which data structure?

1> my %hash-with-arrays = a => [1,2], b => [2,3];
{a => [1 2], b => [2 3]}
>
2> %hash-with-arrays.values>>.map({$_}) #makes a List
((1 2) (2 3))
>
3> %hash-with-arrays.values.map({.flat}) #makes a Seq
((1 2) (2 3))
>

Thank you in advance, Bill.



---------- Forwarded message ---------
Date: Wed, Apr 22, 2020 at 8:58 AM
Subject: Re: unflattering flat
To: perl6-users <perl6-users@perl.org>


Regarding a recent discussion on flattening hash values. Here are some
posted answers:

>#Konrad Bucheli
> my %hash-with-arrays = a => [1,2], b => [2,3];
{a => [1 2], b => [2 3]}
> %hash-with-arrays.values>>.map({$_}) #makes a List
((1 2) (2 3))
> %hash-with-arrays.values>>.map({$_}).flat
(2 3 1 2)
>#ElizabethMattijsen
>  %hash-with-arrays.values.map( { $_.Slip } )
(1 2 2 3)
> say %hash-with-arrays.values.map: |*
(1 2 2 3)
>#Larry Wall
> %hash-with-arrays.values»[].flat
(2 3 1 2)
> say gather %hash-with-arrays.values.deepmap: { .take }
(1 2 2 3)

I played with Raku/Perl6 a bit and found two more constructs on my
own. The first one (A) with {.self} flattens the hash values as
intended.  The second one (B) produces a "Sequence of sequences",
which I don't think I've seen before:

>#Me
A> %hash-with-arrays.values.map({.self}).flat
(1 2 2 3)
>
B> %hash-with-arrays.values.map({.flat}) #makes a Seq
((1 2) (2 3))
> dd(%hash-with-arrays.values.map({.flat}))
((1, 2).Seq, (2, 3).Seq).Seq
Nil
> %hash-with-arrays.values.map({.flat}).WHAT
(Seq)
>

Question: why not a "List of sequences" instead?  Any precedent in
Perl5? What is a "Sequence of sequences" useful for (above and beyond
a "List of sequences")?  How can I predict a priori whether a
particular line of code will return a Seq or a List?

In fact, in comparison to the List generated from Konrad's code, the
auto-printed REPL ".gists" are identical between the .Seq and List
objects above, and it's only upon calling ".elems" that a user sees
that one returns 4 elements while the other returns 2 elements (which
could be confusing).

Thx, Bill.


On Mon, Apr 6, 2020 at 6:20 PM Brad Gilbert <b2gills@gmail.com> wrote:
>
> [*] is also a meta prefix op
>
>     say [*] 4, 3, 2; # 24
>
> But it also looks exactly the same as the [*] postfix combination of operators
>
>     my @a = 1,2,3;
>
>     say @a[*]; # (1 2 3)
>
> There is supposed to be one that looks like [**]
>
>     my @b = [1,], [1,2], [1,2,3];
>
>     say @b[**]; # (1 1 2 1 2 3)
>
> Really @a[*] is
>
>     say postfix:« [ ] »( @a, Whatever )
>
> And @b[**] is
>
>     say postfix:« [ ] »( @b, HyperWhatever )
>
> ---
>
>     say *.WHAT.^name; # Whatever
>
>     say **.WHAT.^name; # HyperWhatever
>
> On Mon, Apr 6, 2020 at 7:05 PM yary <not.com@gmail.com> wrote:
>>
>> Question- what am I missing from the below two replies?
>>
>> Larry's answer came through my browser with munged Unicode, it looks like this
>>
>>
>> - with the Chinese character for "garlic" after the word "values"
>>
>> Then Ralph says "[**] will be a wonderful thing when it's implemented" but as far as I can tell, [**] is exponentiation (math) as a hyper-op, nothing to do with flattening. From https://docs.raku.org/language/operators
>>
>> say [**] 4, 3, 2;     # 4**3**2 = 4**(3**2) = 262144
>>
>>
>>



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About