Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

[Perl 6]: Small discoveries VII, Flattening

by holli (Monsignor)
on Oct 28, 2017 at 23:16 UTC ( #1202244=perlmeditation: print w/replies, xml ) Need Help??

Perl 6 tries to flatten lists. This:
my $a = [[["hello"]]]; #not a 3d array!, same as: ["hello"]
is not what you might think it is, as single element lists get flattened. To get what you mean you must write
my $a = [[["hello"],],]; #now it is!
Note the trailing comma. See also 2015 The Year of The Great List Refactor.


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re: [Perl 6]: Small discoveries VII, Flattening
by QM (Parson) on Oct 31, 2017 at 10:15 UTC
    Note the trailing comma.

    Which is horribly like some Python I've had to write. I hate it when syntax DWIMs the wrong way.

    > python >>> x = (1) >>> x 1 >>> x = (1,) >>> x (1,)

    So (1) is not a tuple, but (1,) is?

    Back to Perl...

    Does the number of trailing commas affect anything? E.g., [[["hello"],,,,],,,,]. (Because in Python it's a syntax error!)

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      So (1) is not a tuple, but (1,) is?
      (1) is a parenthesized expression, not a tuple. Same with (1+1). This is an unfortunate result of having two different meanings for parens.
      Does the number of trailing commas affect anything?
      Consecutive commas are also a syntax error in Perl6.
        Parens don't build lists in Perl6, commas do:
        my @a := 1,2; say @a.^name; #List my @b := 1,; say @b.^name; #List
        Parens are usually needed because of precedence when embedding the List into another expression. The comma does have another meaning: it's the separator in argument lists.
Re: [Perl 6]: Small discoveries VII, Flattening
by Anonymous Monk on Oct 30, 2017 at 15:24 UTC
    ... Why? Is it just for symmetry with List constructor (42,)?
Re: [Perl 6]: Small discoveries VII, Flattening
by Jenda (Abbot) on Dec 29, 2017 at 21:58 UTC

    As if we needed more reasons to ignore the misnamed misdesigned language.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1202244]
Approved by Athanasius
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2018-07-20 03:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (422 votes). Check out past polls.

    Notices?