Re: join questions

2018-10-02 Thread ToddAndMargo

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, ).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 .  And you since passed
.join only two things to stringify and join, it stringified the 1 and
then it stringified the , 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 | 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.

:-)


Re: join questions

2018-10-01 Thread Larry Wall
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, ).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 .  And you since passed
.join only two things to stringify and join, it stringified the 1 and
then it stringified the , 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 | 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


Re: join questions

2018-09-30 Thread Simon Proctor
On Sun, 30 Sep 2018, 12:05 ToddAndMargo,  wrote:

> Hi All,
>
> https://docs.perl6.org/routine/join#(List)_routine_join
>
> method join(List:D: $separator --> Str:D)
>
> $ p6 'say (1, ).join("|");'
> 1|a b c
>
>
> In this instance you have passed in two objects the Int 1 and a list of
> Str's  (<> being similar to qw () in perl5. Join works on Str's to
> the first thing it does is convert each of the two parameters to Str, the
> default Str conversion for a list is space separated hence "a b c" which is
> then joined with "1" to get "1|a b c"
>
>
> It states in the manual that this will happen.
>
> Questions:
>
> 1) why?
>
> 2) where in the method definition does it state
>this will happen?
>
> 3) why does this work?
>   $ p6 'join( "|", <1 2 3>).say;'
>   1|2|3
>
> In this case the join sub will be using the + slurpy so it's flattening
> the given input into it's parameter list and then joining it.
>
> Many thanks,
> -T
>

I think you really want to get a solid understanding of signatures, they
are what gives Perl6 a lot of it's flexibility. Unlike Perl5 where many
functions have special rules handling input in Perl6 a lot of it is done
with Signatures.

Simon

>


join questions

2018-09-30 Thread ToddAndMargo

Hi All,

https://docs.perl6.org/routine/join#(List)_routine_join

method join(List:D: $separator --> Str:D)

$ p6 'say (1, ).join("|");'
1|a b c


It states in the manual that this will happen.

Questions:

   1) why?

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

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

Many thanks,
-T