Fwd: unflattering flat
[email protected] (William Michels via perl6-language) Wed, 29 Apr 2020 16:51:13 -0700
| Newsgroups | perl.perl6.language |
|---|---|
| Message-ID | <CAA99HCyEcAh+ktcWD-Xq9Brr_Cmn8G-akkhCaswm4AD2Q90HMg@mail.gmail.com> |
Migrating this question over from perl6-users <[email protected]> : 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 =3D a =3D> [1,2], b =3D> [2,3]; {a =3D> [1 2], b =3D> [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 <[email protected]> Regarding a recent discussion on flattening hash values. Here are some posted answers: >#Konrad Bucheli > my %hash-with-arrays =3D a =3D> [1,2], b =3D> [2,3]; {a =3D> [1 2], b =3D> [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=C2=BB[].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 <[email protected]> wrote: > > [*] is also a meta prefix op > > say [*] 4, 3, 2; # 24 > > But it also looks exactly the same as the [*] postfix combination of oper= ators > > my @a =3D 1,2,3; > > say @a[*]; # (1 2 3) > > There is supposed to be one that looks like [**] > > my @b =3D [1,], [1,2], [1,2,3]; > > say @b[**]; # (1 1 2 1 2 3) > > Really @a[*] is > > say postfix:=C2=AB [ ] =C2=BB( @a, Whatever ) > > And @b[**] is > > say postfix:=C2=AB [ ] =C2=BB( @b, HyperWhatever ) > > --- > > say *.WHAT.^name; # Whatever > > say **.WHAT.^name; # HyperWhatever > > On Mon, Apr 6, 2020 at 7:05 PM yary <[email protected]> wrote: >> >> Question- what am I missing from the below two replies? >> >> Larry's answer came through my browser with munged Unicode, it looks lik= e this >> >> >> - with the Chinese character for "garlic" after the word "values" >> >> Then Ralph says "[**] will be a wonderful thing when it's implemented" b= ut as far as I can tell, [**] is exponentiation (math) as a hyper-op, nothi= ng to do with flattening. From https://docs.raku.org/language/operators >> >> say [**] 4, 3, 2; # 4**3**2 =3D 4**(3**2) =3D 262144 >> >> >>