On Fri, 21 Apr 2017 08:53:44 -0700, [email protected] wrote:
> On Thu, 20 Apr 2017 08:39:41 -0700, [email protected] wrote:
> > See the following results:
> >
> >
> > $ perl6 -e 'say (1..10).map({ { :a($_) } });'
> > (a => 1 a => 2 a => 3 a => 4 a => 5 a => 6 a => 7 a => 8 a => 9 a =>
> > 10)
> >
> > $ perl6 -e 'say (1..10).map({ ${ :a($_) } });'
> > (-> ;; $_? is raw { #`(Block|57168408) ... } -> ;; $_? is raw {
> > #`(Block|57168480) ... } -> ;; $_? is raw { #`(Block|57168552) ... }
> > -> ;; $_? is raw { #`(Block|57168624) ... } -> ;; $_? is raw {
> > #`(Block|57168696) ... } -> ;; $_? is raw { #`(Block|57168768) ... }
> > -> ;; $_? is raw { #`(Block|57168840) ... } -> ;; $_? is raw {
> > #`(Block|57168912) ... } -> ;; $_? is raw { #`(Block|57168984) ... }
> > -> ;; $_? is raw { #`(Block|57169056) ... })
> >
> >
> > I think the 2nd example should return the same result as the 1st
> > example.
> >
> > $ perl6 --version
> > This is Rakudo version 2017.03-217-ge681498 built on MoarVM version
> > 2017.03-115-ge8231a3
> > implementing Perl 6.c.
> 
> 
> 
> Thank you for the report, however this is not a bug.
> 
> In the first case, the bare block simply gets evaluated right away and
> its result (a Pair) is returned. In the second case, you're itemizing
> that block, so the block itself gets returned. You can achieve the
> same by, say, using a pointy block, which also won't get evaluated
> right away:
> 
> say (1..10).map({ -> { :a($_) } });
> (->  { #`(Block|58087136) ... } ->  .... etc
> 
> I'm only guessing here, but I suspect you wanted the map to return a
> bunch of 1-element hashes? Like:
> 
> say (1..10).map: { %(:a($_)) };
> ({a => 1} {a => 2} {a => 3} {a => 4} {a => 5} {a => 6} {a => 7} {a =>
> 8} {a => 9} {a => 10})
> 
> Note how I used the %() coercer instead of bare curlies. This is due
> to there being an ambiguity in curlies: do they mean a block or a
> hash? When `$_` variable is used inside, the compiler treats it as a
> block, which is why in your case it evaluated the block or returned it
> when you tried to itemize it.
> 
> So another way to get a bunch of 1-element hashes is to not use the
> `$_` inside, but some other variable:
> 
> say (1..10).map: -> $a { {:$a} };
> ({a => 1} {a => 2} {a => 3} {a => 4} {a => 5} {a => 6} {a => 7} {a =>
> 8} {a => 9} {a => 10})
> 
> Cheers,
> ZZ

Thanks for your neat explanation.

> I'm only guessing here, but I suspect you wanted the map to return a bunch of 
> 1-element hashes ?

Yes! I tried to create a bunch of 1-element hashes.
I'm sorry, the 1st example was incorrect for showing what I expected.

Reply via email to