Re: unflattering flat

2020-04-22 Thread William Michels via perl6-users
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  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  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
>>
>>
>>


Re: unflattering flat

2020-04-07 Thread yary
I left out first line in the example REPL

> my %hash-with-arrays = a => [1,2], b => [3,4], c=>5, d=>[6,[7,8]]
{a => [1 2], b => [3 4], c => 5, d => [6 [7 8]]}
> say gather %hash-with-arrays.values.deepmap: { .take }
(6 7 8 1 2 3 4 5)
> say   %hash-with-arrays.values>>[].flat
(6 [7 8] 1 2 3 4 5)


Re: unflattering flat

2020-04-07 Thread yary
Larry all looks good with the latest email and encoding!

Interpreting  %hash-with-arrays.values>>[].flat aka
%hash-with-arrays.values»[].flat

>> is hyperoperator
https://docs.raku.org/language/operators#index-entry-hyper_
<<-hyper_>>-hyper_«-hyper_»-Hyper_operators
and as for [] "it's the postcircumfix[]
 which is being
hypered."
it is "The Array  constructor returns an
itemized Array  that does not flatten in
list context."
And then, call "flat" on the resulting itemized Arrays.

> say gather %hash-with-arrays.values.deepmap: { .take }
(6 7 8 1 2 3 4 5)
> say %hash-with-arrays.values>>[].flat
(6 [7 8] 1 2 3 4 5)

Happy with those!

ps. :-) I now see Ralph's comment including
"microwave+garlic+to+remove+skin" as poetic, referring to both removing the
"item" skins and the Unicode garlic ideogram

-y


On Tue, Apr 7, 2020 at 12:15 PM Larry Wall  wrote:

> On Mon, Apr 06, 2020 at 08:04:45PM -0400, yary wrote:
> : Larry's answer came through my browser with munged Unicode, it looks like
> : this
> :
> : [image: image.png]
> : - with the Chinese character for "garlic" after the word "values"
>
> I wrote the Unicode equivalent of:
>
> %hash-with-arrays.values>>[].flat
>
> but for some reason mutt translated it to latin-1, which your mail program
> apparently doesn't grok.
>
> Maybe if I actually put a Chinese character in like 楽 it will leave it in
> UTF-8?
>
> Testing:
>
> %hash-with-arrays.values»[].flat
>
> Larry
>


Re: unflattering flat

2020-04-07 Thread Larry Wall
On Tue, Apr 07, 2020 at 09:15:06AM -0700, Larry Wall wrote:
: Maybe if I actually put a Chinese character in like 楽 it will leave it in 
UTF-8?

Oops, actually, now that I think about it, 楽 (raku) is a Japanese-only 
character.
The Chinese equivalents are traditional 樂 and simplified 乐.

I really don't know why the browsers would be translating »[ to chinese.
The first message I sent clearly says:

Content-Type: text/plain; charset=iso-8859-1

so the browsers should be able to display a latin-1 » character.  If you
save the message to a file. the file command identifies it as iso-8859,
and vim also interprets it as iso-8859-1, and correctly displays the ».
Catting the file to the terminal doesn't work, so it's not encoded
in utf-8 accidentally.

So I think it has to be your browser or gmail in error...

Larry


Re: unflattering flat

2020-04-07 Thread Larry Wall
On Mon, Apr 06, 2020 at 08:04:45PM -0400, yary wrote:
: Larry's answer came through my browser with munged Unicode, it looks like
: this
: 
: [image: image.png]
: - with the Chinese character for "garlic" after the word "values"

I wrote the Unicode equivalent of:

%hash-with-arrays.values>>[].flat

but for some reason mutt translated it to latin-1, which your mail program
apparently doesn't grok.

Maybe if I actually put a Chinese character in like 楽 it will leave it in UTF-8?

Testing:

%hash-with-arrays.values»[].flat

Larry


Re: unflattering flat

2020-04-06 Thread Brad Gilbert
[*] 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  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
>
> [image: image.png]
> - 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
>
>
>
>


Re: unflattering flat

2020-04-06 Thread yary
Question- what am I missing from the below two replies?

Larry's answer came through my browser with munged Unicode, it looks like
this

[image: image.png]
- 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


Re: unflattering flat

2020-04-04 Thread Larry Wall
Oh, you wanna go deep?  Why stop at 10 levels?

say gather %hash-with-arrays.values.deepmap: { .take }

No .flat needed, even.

Larry

On Sat, Apr 04, 2020 at 10:59:36PM +0100, Ralph Mellor wrote:
: [**] will be a wonderful thing when it's implemented.
: 
: In the meantime, you could maybe use this as a hack that works to 10
: levels deep:
: 
: my %hash-with-arrays = a => [1,2], b => [3,4];
: sub postfix:<[**]> ($arg) { $arg[*;*;*;*;*;*;*;*;*;*]}
: say %hash-with-arrays.values[**].flat # (1 2 3 4)

: 
: On Sat, Apr 4, 2020 at 7:46 PM Larry Wall  wrote:
: >
: > You can also do a hyper descalarize if you're into that sort of thing:
: >
: > %hash-with-arrays.values蒜].flat
: >
: > Larry


Re: unflattering flat

2020-04-04 Thread Ralph Mellor
A microwave is easier:
https://www.google.com/search?q=microwave+garlic+to+remove+skin

[**] will be a wonderful thing when it's implemented.

In the meantime, you could maybe use this as a hack that works to 10
levels deep:

my %hash-with-arrays = a => [1,2], b => [3,4];
sub postfix:<[**]> ($arg) { $arg[*;*;*;*;*;*;*;*;*;*]}
say %hash-with-arrays.values[**].flat # (1 2 3 4)

On Sat, Apr 4, 2020 at 7:46 PM Larry Wall  wrote:
>
> You can also do a hyper descalarize if you're into that sort of thing:
>
> %hash-with-arrays.values蒜].flat
>
> Larry


Re: unflattering flat

2020-04-04 Thread Larry Wall
You can also do a hyper descalarize if you're into that sort of thing:

%hash-with-arrays.values»[].flat

Larry


Re: unflattering flat

2020-04-04 Thread Elizabeth Mattijsen
> On 2 Apr 2020, at 23:49, Konrad Bucheli via perl6-users 
>  wrote:
> I have a hash with arrays as value. Out of that I wanted to get a flat list 
> with all the entries in the value arrays. My first intuitive attempt was to 
> use flat, but somehow that only works with an additional map step:
> 
> $ raku
> To exit type 'exit' or '^D'
>> my %hash-with-arrays = a => [1,2], b => [2,3]
> {a => [1 2], b => [2 3]}
>> %hash-with-arrays.values.flat
> ([2 3] [1 2])
>> %hash-with-arrays.values>>.map({$_})
> ((2 3) (1 2))
>> %hash-with-arrays.values>>.map({$_}).flat
> (2 3 1 2)
>> 
> $ raku -v
> This is Rakudo version 2020.02 built on MoarVM version 2020.02
> implementing Raku 6.d.
> $ 
> 
> How is that explained?

This is because the array inside the hash is inside a container.  You can see 
this is you use `dd` to show the contents of a key:

> dd %hash-with-arrays
Array %hash-with-arrays = $[1, 2]

Note the $ prefix in the output.  This inhibits automatic iteration of the 
array.  There is a simple way around it:

> %h.values.map: |*
(1 2 2 3)

This slightly line-noisy solution is short for:

> %h.values.map( { $_.Slip } )
(1 2 2 3)

Aka, "slip" the values into a single stream of values, hence getting the result 
you wanted

unflattering flat

2020-04-03 Thread Konrad Bucheli via perl6-users
Dear Raku programmers

I have a hash with arrays as value. Out of that I wanted to get a flat list 
with all the entries in the value arrays. My first intuitive attempt was to use 
flat, but somehow that only works with an additional map step:

 $ raku
To exit type 'exit' or '^D'
> my %hash-with-arrays = a => [1,2], b => [2,3]
{a => [1 2], b => [2 3]}
> %hash-with-arrays.values.flat
([2 3] [1 2])
> %hash-with-arrays.values>>.map({$_})
((2 3) (1 2))
> %hash-with-arrays.values>>.map({$_}).flat
(2 3 1 2)
> 
$ raku -v
This is Rakudo version 2020.02 built on MoarVM version 2020.02
implementing Raku 6.d.
$ 

How is that explained?

Cheers

Konrad