perl6 / ye-olde

(ARCHIVE) Out-dated information about Raku
3 stars 2 forks source link

Flattening of arrays #6

Open Rhialto opened 8 years ago

Rhialto commented 8 years ago

rakudo-star-2016.04/share/perl6/doc/Language/syntax.pod says

The array constructor flattens non-itemized arrays and lists, but not
itemized arrays themselves:

    my @a = 1, 2;
    # flattens:
    say [@a, 3, 4].elems;       # 4

    # does not flatten:
    say [[@a], [3, 4]].elems;   # 2

However, I do get 3 instead of 4. In fact, it doesn't flatten:

    > my @a = 1, 2; say [@a, 3, 4]
    [[1 2] 3 4]

On the other hand, these two give the same output ?!?!?!?!

    > say [[@a], [3, 4]]
    [[1 2] [3 4]]
    > say [@a, [3, 4]]
    [[1 2] [3 4]]

It seems that the comments "flattens" and "does not flatten" above are reversed. And the defining sentence above it seems to be reversed too. Somewhat. It is rather confusing to determine what it meant.

In fact, any attempt to put @a into a list, no matter in how many lists I try to nest it, fails:

    > say [[[[@a]]], [3, 4]]
    > [[1 2] [3 4]]

And note the following weird effect, where the flattening of the itemized array (that seems to be what it is) [1,2], depends on what other things are in the containing array:

    > say [[1, 2]]; say [[1,2], 3]
   [1 2]
   [[1 2] 3]

So, how does one create [[[1 2]]] from @a, or from an Array literal?

eritain commented 8 years ago

Array flattening changed in the run-up to version 6.3 and that POD is out of date.

Variables with the @ sigil only autoflatten when they are being bound by a slurpy parameter. Everywhere else, if you want them flattened you have to use the flat operator.

I don't know enough to comment on the [[[1 2]]] issue.