New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
flat can't flatten arrays, but can flatten lists #4979
Comments
From jules@jules.ukflat has no effect on arrays, but works fine on lists:
Running on OS X: Many thanks! |
From @skidsThis is actually a fallout of the interaction of two intentional design features: 1) Arrays itemize (wrap in Scalar) everything assigned to them Which makes it reasonable to ask, why does Array have a ".flat" method if it Indeed this actually does seem to be broken, $ perl6 -e '
Now, if running an Array through an iterator shold return the Scalars, rather If an array should de-itemize during iteration, then Array.flat would behave |
The RT System itself - Status changed from 'new' to 'open' |
From jules@jules.ukBrian, The snag is this:
I asked for a List, but got given an Array. So I can do this:
but not this:
And now @a.flat is a list. But if I assign the result to @b then this
Now it's turned into an Array again! How can it be right that if I split 1 line of code into 2 by creating an Many thanks for your help! Cheers, On 03/01/2016 02:23, Brian S. Julin via RT wrote:
Jules -- 'Ever since the dawn of civilization, people have craved for an -- |
From @lizmat
If like that, you really have asked for an Array (@a) to be assigned with the values of a List (1, (2,3), 4). If you want @a to be a List, you need to *bind* it to the List. $ 6 'my @a = (1, (2,3), 4); say @a.WHAT'
.flat returns a Seq $ 6 'my @a := (1, (2,3), 4); say @a.flat.WHAT'
Again, you need to bind if you want @b to be a List. Because you can’t bind a Seq, it needs to be coerced to a List: $ 6 'my @a = (1, (2,3), 4); my @b := @a.flat.list; say @b.WHAT'
Because the @ sigil forces an Array by default. You don’t have to use an @ sigil though: $ 6 'my $a = (1, (2,3), 4); my $b = $a.flat.list; say $a.WHAT; say $b.WHAT'
Hope this helps… Liz |
From @salortizLiz, IMO the real problem is that, per documentation [ method list: "Interprets the invocant as a list, and returns that List." ], we expect that @foo.list.WHAT === List, ie @foo.list.^name eq 'List' But right now rakudo don't agree: $ perl6 -e 'my ($a,%a,@a); say .WHAT, .list.WHAT === List for I can't found that tested in roast, so where is specified that .list applied to an Array should be a nop? But with an explicit .List: $ perl6 -e 'my ($a,%a,@a); say .List.WHAT === List for $a,%a,@a,[]' Regards. |
From @salortizDigging deeper I finally found some answers. Array.flat was designed and implemented for flattening shaped arrays: $ perl6 -e "my @a[2;2] = [ 'a', 1 ],[ 'b', 2 ]; my @b = @a.flat; .say for @a,@b" So, @foo.flat seems to be a nop iff @foo.shape.elems == 1, as a degenerated case. Iterator.flat and Array.flat are orthogonal: $ perl6 -e "my @a[2;2] = [ 'a', (3,4) ],[ 'b', (5,6) ]; .say for @a, @a.flat, @a.List.flat;" To avoid the confusion doc/Type/Array needs to document shaped arrays and its .flat method. Regards. |
Migrated from rt.perl.org#127121 (status was 'open')
Searchable as RT127121$
The text was updated successfully, but these errors were encountered: