Re: How to push a hash on an array without flattening it to Pairs?

2015-09-26 Thread Aristotle Pagaltzis
* Elizabeth Mattijsen  [2015-09-26 13:20]:
> The flattening will not be done if more than one argument is specified:
>
> $ 6 'my %h = a => 42, b => 666; my @a = %h,%h; dd @a'
> Array @a = [{:a(42), :b(666)}, {:a(42), :b(666)}]
>
>
> This is the same behaviour as with for:
>
> $ 6 'my %h = a => 42, b => 666; dd $_ for %h'
> :a(42)
> :b(666)
>
> $ 6 'my %h = a => 42, b => 666; dd $_ for %h,%h'
> Hash %h = {:a(42), :b(666)}
> Hash %h = {:a(42), :b(666)}
>
>
> It’s the same rule throughout  :-)

Yes, but adding a trailing comma to convert a lone item to an element in
a one-element list, causing the flattening to apply to the list instead
of the item, thus avoiding the flattening of that item as a side effect
of sorts… is just way too meta for my taste, at least for everyday parts
of the language.

> There is: you just need to itemize the hash, e.g. by prefixing it with $
>
> $ 6 'my %h = a => 42, b => 666; my @a = $%h; dd @a'
> Array @a = [{:a(42), :b(666)},]
>
> This is the one argument rule at work.

Aha! Much better. Explicit. “Don’t subject %h to flattening.”

No need to combine two other unrelated rules to bend around invoking the
undesired rule; just directly saying not to invoke it.

Now of course I must ask – is there an opposite also? I.e. when writing
a list, is there a way I can say “do flatten this item?” Or put in other
words, what goes in place of XXX in the following to make it real?

$ 6 'my %h = a => 42, b => 666; dd $_ for %h,XXX'
Hash %h = {:a(42), :b(666)}
:a(42)
:b(666)

Regards,
-- 
Aristotle Pagaltzis // 


Re: How to push a hash on an array without flattening it to Pairs?

2015-09-26 Thread Tobias Leich
Itemization helps:

m: my %h = x => 6, y => 7; my @a = $%h; say @a[0]
rakudo-moar 0132b6: OUTPUT«x => 6, y => 7␤»

m: my %h = x => 6, y => 7; my @a; @a.push: $%h; say @a[0]
rakudo-moar 0132b6: OUTPUT«x => 6, y => 7␤»

Am 26.09.2015 um 07:58 schrieb Gabor Szabo:
> In the first two cases the hash was converted to Pairs before
> assigning to the array.
> Only the third case gave what I hoped for. How can I push a hash onto
> an array as a single entity?
>
>
> use v6;
>
> my %h = x => 6, y => 7;
> say %h.perl; #  {:x(6), :y(7)}
>
> my @a = %h;
> say @a.elems;   #
> say @a[0]; # x => 6
>
>
>
> my @c;
> @c.push(%h);
> say @c.elems; # 2
> say @c[0];   # x => 6
>
>
> my @b;
> @b[@b.elems] = %h;
> say @b.elems;  # 1
> say @b[0];# x => 6, y => 7
>
>



Re: How to push a hash on an array without flattening it to Pairs?

2015-09-26 Thread Aristotle Pagaltzis
* Moritz Lenz  [2015-09-26 09:40]:
> A trailing comma helps:
>
> my %h = a => 1, b => 2;
> my @a = %h, ;
> say @a.perl;# [{:a(1), :b(2)},]

I think I understand why, but wow, that’s not reasonable. Is there
really no better way to avoid the flattening? Even Perl 5 is nicer
in that situation…

-- 
Aristotle Pagaltzis // 


Re: How to push a hash on an array without flattening it to Pairs?

2015-09-26 Thread Elizabeth Mattijsen
> On 26 Sep 2015, at 13:09, Aristotle Pagaltzis  wrote:
> * Moritz Lenz  [2015-09-26 09:40]:
>> A trailing comma helps:
>> 
>> my %h = a => 1, b => 2;
>> my @a = %h, ;
>> say @a.perl;# [{:a(1), :b(2)},]
> 
> I think I understand why, but wow, that’s not reasonable. Is there
> really no better way to avoid the flattening? Even Perl 5 is nicer
> in that situation…

There is: you just need to itemize the hash, e.g. by prefixing it with $

$ 6 'my %h = a => 42, b => 666; my @a = $%h; dd @a'
Array @a = [{:a(42), :b(666)},]

This is the one argument rule at work.


The flattening will not be done if more than one argument is specified:

$ 6 'my %h = a => 42, b => 666; my @a = %h,%h; dd @a'
Array @a = [{:a(42), :b(666)}, {:a(42), :b(666)}]


This is the same behaviour as with for:

$ 6 'my %h = a => 42, b => 666; dd $_ for %h'
:a(42)
:b(666)

$ 6 'my %h = a => 42, b => 666; dd $_ for %h,%h'
Hash %h = {:a(42), :b(666)}
Hash %h = {:a(42), :b(666)}


It’s the same rule throughout  :-)


Liz

Re: How to push a hash on an array without flattening it to Pairs?

2015-09-26 Thread Moritz Lenz

On 09/26/2015 02:26 PM, Aristotle Pagaltzis wrote:
> Now of course I must ask – is there an opposite also? I.e. when writing
> a list, is there a way I can say “do flatten this item?” 

Yes, that's what type Slip is for: http://doc.perl6.org/type/Slip

It's useful for returning more than one list item from a .map call, for
example.

Cheers,
Moritz


Re: How to push a hash on an array without flattening it to Pairs?

2015-09-26 Thread Elizabeth Mattijsen
> On 26 Sep 2015, at 14:26, Aristotle Pagaltzis  wrote:
> * Elizabeth Mattijsen  [2015-09-26 13:20]:
>> There is: you just need to itemize the hash, e.g. by prefixing it with $
>> 
>> $ 6 'my %h = a => 42, b => 666; my @a = $%h; dd @a'
>> Array @a = [{:a(42), :b(666)},]
>> 
>> This is the one argument rule at work.
> 
> Aha! Much better. Explicit. “Don’t subject %h to flattening.”
> 
> No need to combine two other unrelated rules to bend around invoking the
> undesired rule; just directly saying not to invoke it.
> 
> Now of course I must ask – is there an opposite also? I.e. when writing
> a list, is there a way I can say “do flatten this item?” Or put in other
> words, what goes in place of XXX in the following to make it real?
> 
>$ 6 'my %h = a => 42, b => 666; dd $_ for %h,XXX'
>Hash %h = {:a(42), :b(666)}
>:a(42)
>:b(666)

moritz++ already mentioned it:

$ 6 'my %h = a => 42, b => 666; dd $_ for |%h,|%h'
:a(42)
:b(666)
:a(42)
:b(666)

Prefix | is a short way to Slip something :-)



Liz

How to push a hash on an array without flattening it to Pairs?

2015-09-25 Thread Gabor Szabo
In the first two cases the hash was converted to Pairs before assigning to
the array.
Only the third case gave what I hoped for. How can I push a hash onto an
array as a single entity?


use v6;

my %h = x => 6, y => 7;
say %h.perl; #  {:x(6), :y(7)}

my @a = %h;
say @a.elems;   #
say @a[0]; # x => 6



my @c;
@c.push(%h);
say @c.elems; # 2
say @c[0];   # x => 6


my @b;
@b[@b.elems] = %h;
say @b.elems;  # 1
say @b[0];# x => 6, y => 7