On 10/1/18 4:35 PM, Larry Wall wrote:
On Sun, Sep 30, 2018 at 04:02:15AM -0700, ToddAndMargo wrote:
: Hi All,
:
: https://docs.perl6.org/routine/join#(List)_routine_join
:
: method join(List:D: $separator --> Str:D)
:
: $ p6 'say (1, <a b c>).join("|");'
: 1|a b c
:
:
: It states in the manual that this will happen.
:
: Questions:
:
:    1) why?

Because an invocant is only one thing.  In this case, it's a List of
exactly two things: a 1 and a sublist of <a b c>.  And you since passed
.join only two things to stringify and join, it stringified the 1 and
then it stringified the <a b c>, and then it joined those two things,
just as you asked it to.

A deeper "why" is that Perl 6 doesn't autoflatten sublists unless you
explicitly ask it to, so you'd need to put |<a b c> to "slip" the sublist
into the outer list, or use flat().

:    2) where in the method definition does it state
:       this will happen?

It's not in the method definition, and it doesn't belong in the method
definition.  It's in the definition of how Perl 6 works at a fundamental
level.  We're not going to document every list argument in Perl 6 to
say that it *doesn't* work like in Perl 5.

:    3) why does this work?
:          $ p6 'join( "|", <1 2 3>).say;'
:          1|2|3

Because functions can take multiple arguments, and so the signature of
the join function is allowed (and required) to use a comma to separate
those arguments.  This is not the same comma that creates the general
list above.  Functions often cheat with their commas, especially functions
that were borrowed from Perl 5, which cheats on most of its commas and
doesn't really understand nested lists at all without explicit references.

The deeper and/or shallower reason is that a Perl 5 programmer will
expect the join function to work that way, and even expect the slurpy
argument to autoflatten, which it does for old time's sake, and because
it seems to be a useful DWIM for this particular string-based function.
The functions that flatten their slurpy (like join) are explicitly marked
with * on that argument in the signature.  So it's still true that we
flatten only explicitly in Perl 6, but that explicit mark is in the
that pesky signature you gotta get your brain around someday.

In short, the method form is optimized for people who understand nested
lists and want them to to stay nested by default, while the function
form is optimized for, er, the typical Perl 5 programmer who expects
random list flattening in various places.

Larry


Thank you!  I understand now.

:-)

Reply via email to