develooper Front page | perl.perl6.users | Postings from July 2021

Re: [naive] hash assingment

Thread Previous | Thread Next
From:
Vadim Belman
Date:
July 15, 2021 13:55
Subject:
Re: [naive] hash assingment
Message ID:
634C9113-0B50-4D75-9798-CCE849EA9C91@lflat.org

The discussion took an interesting route. :)

The problem with an array in a hash is the problem of hashes implicitly itemizing values. Consider the example:

⇒ raku -e 'my %h = a => [1,2,3,4]; say %h<a>.raku; say (%h<a> ==> map({ sqrt $_ })); %h<a> := [1,2,3,4]; say %h<a>.raku; say (%h<a> ==> map({ sqrt $_ }))'
$[1, 2, 3, 4]
(2)
[1, 2, 3, 4]
(1 1.4142135623730951 1.7320508075688772 2)

Sorry for one-liner. The point of the example is that binding to a key avoids itemization of its RHS if it is a non-itemized object.

Because itemizing in fact means putting things into a Scalar container, then the fastest thing which resolves the issue with ==> would be de-containerize a value:

⇒ raku -e 'my %h = a => [1,2,3,4]; say %h<a>.raku; say (%h<a> ==> map({ sqrt $_ })); say (%h<a><> ==> map({ sqrt $_ }));'
$[1, 2, 3, 4]
(2)
(1 1.4142135623730951 1.7320508075688772 2)

<> here is the decontainerization operator. And it would be the fastest solution because most others are coercers meaning that the most likely outcome of .Slip, .List, pipe operator, etc. – would be creation of a new object. With <> we just pull out the Array from the container with no modifications or copying.

Note that I say nothing to justify the use of ==>. I simply don't use it.

Best regards,
Vadim Belman

> On Jul 14, 2021, at 8:13 PM, Brad Gilbert <b2gills@gmail.com> wrote:
> 
> Honestly I would advise against using ==> at the moment.
> 
> For one thing it doesn't even work like it is intended.
> Each side of it is supposed to act like a separate process.
> 
> There are also issues with the syntax that are LTA.
> The fact that you have to tell it the left side is actually a list is one such issue.
> 
> It isn't even all that clearer than just using a method call
> 
>     > %a<column1>         .map({.sqrt});
> 
> ---
> 
> Using 「.Slip」 or 「|」 prefix works, but is the wrong thing.
> You need to tell it that it is a list, so use 「.List」 or 「@(…)」
> 
>     > @(%a<column1>) ==> map({.sqrt})
>     > %a<column1>.List ==> map({.sqrt})
> 
> Since a Slip is a type of List, using it works, but for the wrong reasons.
> 
> 
> On Wed, Jul 14, 2021 at 2:58 PM Aureliano Guedes <guedes.aureliano@gmail.com <mailto:guedes.aureliano@gmail.com>> wrote:
> thank
> 
> It is now more clear.
> And I like this notation |%a<column1> ==> map({.sqrt});
> less is more sometimes
> 
> 
> 
> On Wed, Jul 14, 2021 at 4:41 PM Daniel Sockwell <daniel@codesections.com <mailto:daniel@codesections.com>> wrote:
> To expand slightly on what Clifton said, the reason that
> 
> > %a<column3> = %a<column1>.map: { .sqrt };
> > # (1 1.4142135623730951 1.7320508075688772 2 2.23606797749979)
> 
> does what you mean but 
> 
> > %a{'column1'} ==> map( { .sqrt } )
> > # (2.23606797749979)
> 
> does not is that the method .map maps over *each item* in the Array, whereas
> ==> map maps over the Array as *one collection*.  When taking the square root,
> an Array needs to be treated as an number, which for Raku means treating it as 
> a count of how many elements it has (i.e., its length).
> 
> So `%a{'column1'} ==> map({.sqrt})` is the same as `%a{'column1'}.elems.map({.sqrt})`
> 
> If want to map over each item in the Array when using the ==> operator, you need to
> slip the items out of the Array before feeding them on.  You can do that with either
> of the following (equivalent) lines:
> 
> > %a{'column1'}.Slip ==> map({.sqrt});
> > |%a{'column1>'}==> map({.sqrt});
> 
> (Also, you may already know this, but when the keys of your hash are strings, you 
> can write %a<column1> instead of %a{'column1'}  )
> 
> Hope that helps!
> 
> –codesections
> 
> 
> -- 
> Aureliano Guedes
> skype: aureliano.guedes
> contato:  (11) 94292-6110
> whatsapp +5511942926110


Thread Previous | Thread Next


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