Re: **@args question

2019-01-02 Thread ToddAndMargo via perl6-users

On 1/2/19 10:17 PM, ToddAndMargo via perl6-users wrote:

 > On Wed, Jan 2, 2019 at 8:41 PM ToddAndMargo via perl6-users
 >  wrote:
 >>
 >> Hi All,
 >>
 >> Looking at
 >>
 >>  https://docs.perl6.org/routine/print
 >>
 >> I see
 >>
 >>  multi sub print(**@args --> True)
 >>
 >> Question.  If I wanted to create my own print routine
 >> using **@args, how would I declare it?
 >>
 >>  sub printx( **@args data ) {...}
 >>
 >>
 >> Many thanks,
 >> -T


On 1/2/19 10:06 PM, Brad Gilbert wrote:

The variable name in :(**@args) is @args, it could be any valid array
identifier like @data

 sub printx( **@data ){…}

Note that there are several forms for slurpy positional arguments.

 :(   @a ) # not slurpy at all, takes one Positional argument and
aliases it as @a.

 :(  *@a ) # flattening slurpy  (1,),2,3 → 1,2,3 and ((1,),2,3) → 
1,2,3

 :( **@a ) # structured slurpy (1,),2,3 → (1,),2,3 and ((1,),2,3) →
((1,),2,3) # note there is no change
 :(  +@a ) # one-arg rule slurpy (1,),2,3 → (1,),2,3 and ((1,),2,3)
→ (1,),2,3 # note that the second one changes

Note that itemized values do not get flattened for :(*@a), and that
Array values are itemized.

I like to consider the one-arg rule slurpy :(+@a) to be like a
combination between non-slurpy :(@a) and structured slurpy :(**@a)
That is a one-arg rule slurpy will sometimes be like an alias to a
singular positional, otherwise it will act like a structured slurpy.

Note that all but the [aliasing] non-slurpy :(@a) are [almost] always 
Array's.


---

The one-arg rule slurpy is the oddest one of the bunch so here is some
brief information:

The one-arg rule slurpy can be sigiless :(+a), in which case it will
be a List instead of an Array or an alias to the single argument
depending on what it was.

 sub one-arg-rule ( +args ){
 say args.^name, " # ", args.perl;
 }

 one-arg-rule(  (1,),2,3  ); # List # ((1,), 2, 3)
 one-arg-rule( ((1,),2,3) ); # List # ((1,), 2, 3) # one-arg rule
 one-arg-rule( ((1,),2,3).Seq ); # Seq # ((1,), 2, 3).Seq # 
one-arg rule

 one-arg-rule(  1..3   ); # List # (1, 2, 3)
 one-arg-rule( (1..3,) ); # List # (1..3,)

 sub one-arg-Array ( +@args ){
 say @args.^name, " # ", @args.perl;
 }

 one-arg-Array(  (1,),2,3  ); # Array # [(1,), 2, 3]
 one-arg-Array( ((1,),2,3) ); # Array # [(1,), 2, 3]
 one-arg-Array( ((1,),2,3).Seq ); # List # ((1,), 2, 3)
 one-arg-Array(  1..3   ); # Array # [1, 2, 3]
 one-arg-Array( (1..3,) ); # Array # [1..3,]

The one-arg rule exists because people tend to write the following:

 my @a = [1,2,3];

When the correct way to write it is:

 my @a = 1,2,3;

There are various other places where the one-arg rule is also used, so
it was made available to everyone.

---

Don't get too flustered if you don't understand all of the nuances, it
take everybody a while to understand them.



Thank you!

The term "slurpy" did help a lot.

:-)

I am writing your explanation down for my records.



Well Golly!

$ p6 'sub printx(**@args){print(@args)}; printx("abc",1,"def\n");'
abc 1 def

$ p6 'sub printx(**@args){print @args, "\n"}; printx("abc",1,"def");'
abc 1 def

Question:
$ p6 'sub printx(**@args){print(@args)}; printx("abc","xyz","def\n");'
abc xyz def

Are the spaces suppose to be there?


Re: **@args question

2019-01-02 Thread ToddAndMargo via perl6-users

> On Wed, Jan 2, 2019 at 8:41 PM ToddAndMargo via perl6-users
>  wrote:
>>
>> Hi All,
>>
>> Looking at
>>
>>  https://docs.perl6.org/routine/print
>>
>> I see
>>
>>  multi sub print(**@args --> True)
>>
>> Question.  If I wanted to create my own print routine
>> using **@args, how would I declare it?
>>
>>  sub printx( **@args data ) {...}
>>
>>
>> Many thanks,
>> -T


On 1/2/19 10:06 PM, Brad Gilbert wrote:

The variable name in :(**@args) is @args, it could be any valid array
identifier like @data

 sub printx( **@data ){…}

Note that there are several forms for slurpy positional arguments.

 :(   @a ) # not slurpy at all, takes one Positional argument and
aliases it as @a.

 :(  *@a ) # flattening slurpy  (1,),2,3 → 1,2,3 and ((1,),2,3) → 1,2,3
 :( **@a ) # structured slurpy (1,),2,3 → (1,),2,3 and ((1,),2,3) →
((1,),2,3) # note there is no change
 :(  +@a ) # one-arg rule slurpy (1,),2,3 → (1,),2,3 and ((1,),2,3)
→ (1,),2,3 # note that the second one changes

Note that itemized values do not get flattened for :(*@a), and that
Array values are itemized.

I like to consider the one-arg rule slurpy :(+@a) to be like a
combination between non-slurpy :(@a) and structured slurpy :(**@a)
That is a one-arg rule slurpy will sometimes be like an alias to a
singular positional, otherwise it will act like a structured slurpy.

Note that all but the [aliasing] non-slurpy :(@a) are [almost] always Array's.

---

The one-arg rule slurpy is the oddest one of the bunch so here is some
brief information:

The one-arg rule slurpy can be sigiless :(+a), in which case it will
be a List instead of an Array or an alias to the single argument
depending on what it was.

 sub one-arg-rule ( +args ){
 say args.^name, " # ", args.perl;
 }

 one-arg-rule(  (1,),2,3  ); # List # ((1,), 2, 3)
 one-arg-rule( ((1,),2,3) ); # List # ((1,), 2, 3) # one-arg rule
 one-arg-rule( ((1,),2,3).Seq ); # Seq # ((1,), 2, 3).Seq # one-arg rule
 one-arg-rule(  1..3   ); # List # (1, 2, 3)
 one-arg-rule( (1..3,) ); # List # (1..3,)

 sub one-arg-Array ( +@args ){
 say @args.^name, " # ", @args.perl;
 }

 one-arg-Array(  (1,),2,3  ); # Array # [(1,), 2, 3]
 one-arg-Array( ((1,),2,3) ); # Array # [(1,), 2, 3]
 one-arg-Array( ((1,),2,3).Seq ); # List # ((1,), 2, 3)
 one-arg-Array(  1..3   ); # Array # [1, 2, 3]
 one-arg-Array( (1..3,) ); # Array # [1..3,]

The one-arg rule exists because people tend to write the following:

 my @a = [1,2,3];

When the correct way to write it is:

 my @a = 1,2,3;

There are various other places where the one-arg rule is also used, so
it was made available to everyone.

---

Don't get too flustered if you don't understand all of the nuances, it
take everybody a while to understand them.



Thank you!

The term "slurpy" did help a lot.

:-)

I am writing your explanation down for my records.


Re: **@args question

2019-01-02 Thread Brad Gilbert
The variable name in :(**@args) is @args, it could be any valid array
identifier like @data

sub printx( **@data ){…}

Note that there are several forms for slurpy positional arguments.

:(   @a ) # not slurpy at all, takes one Positional argument and
aliases it as @a.

:(  *@a ) # flattening slurpy  (1,),2,3 → 1,2,3 and ((1,),2,3) → 1,2,3
:( **@a ) # structured slurpy (1,),2,3 → (1,),2,3 and ((1,),2,3) →
((1,),2,3) # note there is no change
:(  +@a ) # one-arg rule slurpy (1,),2,3 → (1,),2,3 and ((1,),2,3)
→ (1,),2,3 # note that the second one changes

Note that itemized values do not get flattened for :(*@a), and that
Array values are itemized.

I like to consider the one-arg rule slurpy :(+@a) to be like a
combination between non-slurpy :(@a) and structured slurpy :(**@a)
That is a one-arg rule slurpy will sometimes be like an alias to a
singular positional, otherwise it will act like a structured slurpy.

Note that all but the [aliasing] non-slurpy :(@a) are [almost] always Array's.

---

The one-arg rule slurpy is the oddest one of the bunch so here is some
brief information:

The one-arg rule slurpy can be sigiless :(+a), in which case it will
be a List instead of an Array or an alias to the single argument
depending on what it was.

sub one-arg-rule ( +args ){
say args.^name, " # ", args.perl;
}

one-arg-rule(  (1,),2,3  ); # List # ((1,), 2, 3)
one-arg-rule( ((1,),2,3) ); # List # ((1,), 2, 3) # one-arg rule
one-arg-rule( ((1,),2,3).Seq ); # Seq # ((1,), 2, 3).Seq # one-arg rule
one-arg-rule(  1..3   ); # List # (1, 2, 3)
one-arg-rule( (1..3,) ); # List # (1..3,)

sub one-arg-Array ( +@args ){
say @args.^name, " # ", @args.perl;
}

one-arg-Array(  (1,),2,3  ); # Array # [(1,), 2, 3]
one-arg-Array( ((1,),2,3) ); # Array # [(1,), 2, 3]
one-arg-Array( ((1,),2,3).Seq ); # List # ((1,), 2, 3)
one-arg-Array(  1..3   ); # Array # [1, 2, 3]
one-arg-Array( (1..3,) ); # Array # [1..3,]

The one-arg rule exists because people tend to write the following:

my @a = [1,2,3];

When the correct way to write it is:

my @a = 1,2,3;

There are various other places where the one-arg rule is also used, so
it was made available to everyone.

---

Don't get too flustered if you don't understand all of the nuances, it
take everybody a while to understand them.

On Wed, Jan 2, 2019 at 8:41 PM ToddAndMargo via perl6-users
 wrote:
>
> Hi All,
>
> Looking at
>
> https://docs.perl6.org/routine/print
>
> I see
>
> multi sub print(**@args --> True)
>
> Question.  If I wanted to create my own print routine
> using **@args, how would I declare it?
>
> sub printx( **@args data ) {...}
>
>
> Many thanks,
> -T
>
>
> --
> ~~
> When you say, "I wrote a program that
> crashed Windows," people just stare at
> you blankly and say, "Hey, I got those
> with the system, for free."
>   -- Linus Torvalds
> ~~


**@args question

2019-01-02 Thread ToddAndMargo via perl6-users

Hi All,

Looking at

   https://docs.perl6.org/routine/print

I see

   multi sub print(**@args --> True)

Question.  If I wanted to create my own print routine
using **@args, how would I declare it?

   sub printx( **@args data ) {...}


Many thanks,
-T


--
~~
When you say, "I wrote a program that
crashed Windows," people just stare at
you blankly and say, "Hey, I got those
with the system, for free."
 -- Linus Torvalds
~~


Re: Bad syntax check

2019-01-02 Thread ToddAndMargo via perl6-users

On 1/2/19 4:00 PM, Brad Gilbert wrote:

You can only list one type as a return type.

If there were a hypothetical Tuple type:

     sub AddThree( Int $a, Int $b, Int $c --> Tuple[Str, Int] {
         my Int $d = $a + $b + $c;
         return Tuple[Str,Int].new( "a+b+c=", $d );
     }


I drew the line at creating new types


Re: Bad syntax check

2019-01-02 Thread Brad Gilbert
You can only list one type as a return type.

If there were a hypothetical Tuple type:

sub AddThree( Int $a, Int $b, Int $c --> Tuple[Str, Int] {
my Int $d = $a + $b + $c;
return Tuple[Str,Int].new( "a+b+c=", $d );
}


my Int $X = 0;
my Int $Y = 0;
my Str $Z;

Tuple[Str,Int].new( $X, $Y, $Y ) = AddThree( 1, 2, 3 );
say "X = <$X>\tY = <$Y>\tZ = <$Z>";

 There could be a circumfix operator to generate the Tuple objects.

You are dealing with Lists, which don't have a type specifier for their
elements.

On Wed, Jan 2, 2019 at 4:51 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 1/2/19 2:27 PM, ToddAndMargo via perl6-users wrote:
> > On 1/2/19 2:11 PM, Simon Proctor wrote:
> >> Have you tried defining your return values in the signature?
> >>
> >> sub AddThree( Int $a, Int $b, Int $c --> Int) {...}
> >>
> >> With this the compiler knows what your function is supposed to return
> >> and can earn you in advance.
> >
> > I did and it blew up in my face so I stopped doing it.
>
> For example:
>
> $ cat RtnBooBoo2.pl6
> #!/usr/bin/env perl6
>
> sub AddThree( Int $a, Int $b, Int $c --> Str, Int ) {
> my Int $d = $a + $b + $c;
> return ( "a+b+c=", $d );
> }
>
> my Int $X = 0;
> my Int $Y = 0;
> my Str $Z;
>
> ( $X, $Y, $Y ) = AddThree( 1, 2, 3 );
> say "X = <$X>\tY = <$Y>\tZ = <$Z>";
>
>
>
> RtnBooBoo2.pl6
> ===SORRY!=== Error while compiling /home/linuxutil/./RtnBooBoo2.pl6
> Malformed return value (return constraints only allowed at the end of
> the signature)
> at /home/linuxutil/./RtnBooBoo2.pl6:3
> --> AddThree( Int $a, Int $b, Int $c --> Str⏏, Int ) {
>


Re: Bad syntax check

2019-01-02 Thread ToddAndMargo via perl6-users

On 1/2/19 2:27 PM, ToddAndMargo via perl6-users wrote:

On 1/2/19 2:11 PM, Simon Proctor wrote:

Have you tried defining your return values in the signature?

sub AddThree( Int $a, Int $b, Int $c --> Int) {...}

With this the compiler knows what your function is supposed to return 
and can earn you in advance.


I did and it blew up in my face so I stopped doing it.


For example:

$ cat RtnBooBoo2.pl6
#!/usr/bin/env perl6

sub AddThree( Int $a, Int $b, Int $c --> Str, Int ) {
   my Int $d = $a + $b + $c;
   return ( "a+b+c=", $d );
}

my Int $X = 0;
my Int $Y = 0;
my Str $Z;

( $X, $Y, $Y ) = AddThree( 1, 2, 3 );
say "X = <$X>\tY = <$Y>\tZ = <$Z>";



RtnBooBoo2.pl6
===SORRY!=== Error while compiling /home/linuxutil/./RtnBooBoo2.pl6
Malformed return value (return constraints only allowed at the end of 
the signature)

at /home/linuxutil/./RtnBooBoo2.pl6:3
--> AddThree( Int $a, Int $b, Int $c --> Str⏏, Int ) {


Re: Basic question about lexical binding in relationship with "list assignment"

2019-01-02 Thread Elizabeth Mattijsen
Fixed with https://github.com/rakudo/rakudo/commit/23663609a7

> On 27 Dec 2018, at 09:29, Raymond Dresens  wrote:
> 
> Hello,
> 
> I'm getting up to speed with Perl 6 again after a long while, and I more
> or less 'hit my nose' against something that I can reduce to the
> following very basic snippet of code:
> 
>my $foo = 3;
>say $foo;
> 
>{
>say $foo;
> 
>my $foo = 6;
> 
>say $foo;
>}
> 
> This will not compile with Perl 6 (Rakudo 2018.12) -- lexical symbol
> '$foo' is already bound to an outer symbol; the implicit outer binding
> must be rewritten as OUTER::<$foo> before you can unambiguously declare
> a new '$foo' in this scope.
> 
> This will compile with Perl 5 when I add 'use v5.14;' at the top and it
> will then print 3, then 3 then 6 (as expected).
> 
> Well, it seems that I can 'cheat' by simply doing a list assignment:
> 
>my $foo = 3;
>say $foo;
> 
>{
>say $foo;
> 
>my ($foo) = 6; # avoid `... '$foo' is already bound ...'
> 
>say $foo;
>say $foo.WHAT; # is it really an integer?
>}
> 
> The output is:
> 
>3
>(Any)
>6
>(Int)
> 
> This behavior is fine I think (but a little unexpected due to my
> experience with Perl 5).
> 
> Is this behavior normal/wanted/intented (from a language/compiler
> perspective?).
> 
> Perhaps this shouldn't be and the compiler should also complain in this
> list assignment case, or perhaps generate a warning?
> 
> I'm not that confident to state that this is a bug ;)
> 
> Hence the reason why I kind-of report this,
> 
> I hope this is somewhat useful,
> 
> Thanks for your time,
> 
> Regards,
> 
> Raymond Dresens.


Re: Bad syntax check

2019-01-02 Thread ToddAndMargo via perl6-users

On 1/2/19 2:11 PM, Simon Proctor wrote:

Have you tried defining your return values in the signature?

sub AddThree( Int $a, Int $b, Int $c --> Int) {...}

With this the compiler knows what your function is supposed to return 
and can earn you in advance.


I did and it blew up in my face so I stopped doing it.


Re: Bad syntax check

2019-01-02 Thread Simon Proctor
Have you tried defining your return values in the signature?

sub AddThree( Int $a, Int $b, Int $c --> Int) {...}

With this the compiler knows what your function is supposed to return and
can earn you in advance.

On Wed, 2 Jan 2019, 20:04 ToddAndMargo via perl6-users  Dear Perl 6 Developers,
>
> Fedora 29, x64
> Xfce 4.13
>
> $ perl6 -v
>  This is Rakudo version 2018.11 built on MoarVM version
>  2018.11 implementing Perl 6.d.
>
> I am constantly improving (changing things) in my subs,
> etc..  As such, the things I return often change.
>
> Because of this, I have a found something I just do not like
> in the checker (Perl6 -c xxx) and the run time compiler.
>
> Here is a simplified sample code with the error in it:
>
> 
> #!/usr/bin/env perl6
>
> sub AddThree( Int $a, Int $b, Int $c ) {
> my Int $d = $a + $b + $c;
> return $d;
> }
>
> my Int $X = 0;
> my Int $Y = 0;
>
> ( $X, $Y ) = AddThree( 1, 2, 3 );
> say "X = <$X>\tY = <$Y>";
> 
>
>
> The error is that the subroutine is only returning one
> value and two are trying to be read.
>
> And the checker passes it!
>
>  $ perl6 -c RtnBooBoo.pl6
>  Syntax OK
>
> No, it is not okay.  The sub and the caller do not match up!
>
>
> If you run the flawed code, you get:
>
>  $ RtnBooBoo.pl6
>  Use of uninitialized value of type Int in string context.
>  Methods .^name, .perl, .gist, or .say can be used to stringify
>  it to something meaningful.  in block  at ./RtnBooBoo.pl6
>  line 12
>
>  X = <6>Y = <>
>
> Which is a bizarre warning (it does not stop on this error).  And
> it is also not the error.  The error was that the return line's
> returned variables and the caller do not match up.  Not an
> uninitialized value.
>
> If you send too few variables to a sub, you get the finger shaken
> at you.  The return should also have the same checking capability.
>
> Would you guys please consider improving the checker and the compiler?
>
> Many thanks,
> -T
>


Bad syntax check

2019-01-02 Thread ToddAndMargo via perl6-users

Dear Perl 6 Developers,

Fedora 29, x64
Xfce 4.13

$ perl6 -v
This is Rakudo version 2018.11 built on MoarVM version
2018.11 implementing Perl 6.d.

I am constantly improving (changing things) in my subs,
etc..  As such, the things I return often change.

Because of this, I have a found something I just do not like
in the checker (Perl6 -c xxx) and the run time compiler.

Here is a simplified sample code with the error in it:


#!/usr/bin/env perl6

sub AddThree( Int $a, Int $b, Int $c ) {
   my Int $d = $a + $b + $c;
   return $d;
}

my Int $X = 0;
my Int $Y = 0;

( $X, $Y ) = AddThree( 1, 2, 3 );
say "X = <$X>\tY = <$Y>";



The error is that the subroutine is only returning one
value and two are trying to be read.

And the checker passes it!

$ perl6 -c RtnBooBoo.pl6
Syntax OK

No, it is not okay.  The sub and the caller do not match up!


If you run the flawed code, you get:

$ RtnBooBoo.pl6
Use of uninitialized value of type Int in string context.
Methods .^name, .perl, .gist, or .say can be used to stringify
it to something meaningful.  in block  at ./RtnBooBoo.pl6
line 12

X = <6>   Y = <>

Which is a bizarre warning (it does not stop on this error).  And
it is also not the error.  The error was that the return line's
returned variables and the caller do not match up.  Not an
uninitialized value.

If you send too few variables to a sub, you get the finger shaken
at you.  The return should also have the same checking capability.

Would you guys please consider improving the checker and the compiler?

Many thanks,
-T


Re: Pair colon-chains

2019-01-02 Thread yary
Copied to https://github.com/perl6/roast/issues/507
-y


On Tue, Dec 25, 2018 at 4:09 AM JJ Merelo  wrote:

> Probably not. Can you please raise an issue in perl6/doc? The first one
> seems specially inconsistent...
> We'll have to check also if it's REPL only or it extends somewhere else...
>
> El mar., 25 dic. 2018 a las 1:23, yary () escribió:
>
>> https://docs.perl6.org/type/Pair says "Colon pairs can be chained
>> without a comma to create a List of Pairs. Depending on context you may
>> have to be explicit when assigning colon lists." - and follows with
>> examples, which led me to experiments, that raise more questions. I would
>> think all 5 of these should say the same thing but instead give 3 different
>> answers. Does the doc need an addition to describe this? Is it a problem
>> with Rakudo Star 2018.10?
>>
>> $ perl6
>>
>> To exit type 'exit' or '^D'
>>
>> > (:a1:b2:c3)
>>
>> (a1 => True b2 => 1 c3 => 1)
>>
>> > (:a1,:b2,:c3)
>>
>> (a1 => True b2 => True c3 => True)
>>
>> > :a1,:b2,:c3
>>
>> (a1 => True b2 => True c3 => True)
>>
>> > :a1:b2:c3
>>
>> a1 => True
>>
>> > :a1 :b2 :c3
>>
>> a1 => True
>>
>>
>>
>> -y
>>
>
>
> --
> JJ
>