Skip to content
This repository was archived by the owner on Feb 3, 2021. It is now read-only.
This repository was archived by the owner on Feb 3, 2021. It is now read-only.

Flattening of arrays #6

@Rhialto

Description

@Rhialto

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions