new method question

2008-08-22 Thread Xiao Yafeng
There are no barewords in Perl 6, but it seems new method is an exception:

class Dog {

has $name;
method bark () {
say $name;
}
}
my $p = Dog.new($name => 'boo');
  $p.bark;#error!
my $p = Dog.new( name => 'boo');
  $p.bark#say boo

more confused:

class Dog {

has @names;
method bark () {
say @names;
}
}
my $p = Dog.new(names => 'boo');
  $p.bark;#nothing but passed.

So, how set array attribute of a class by new method?


whats wrong with this code?

2008-08-22 Thread Andy Colson

Hi List --

I'v started playing around with perl 6, and I am having problems with 
this example:


use v6;

sub xsum (@list)
{
my $i = 0;
print "summing: ";
for @list
{
$i += $_;
print $_,",";
}
say " = $i";
return $i;
}
say "sum = ", xsum( (1,2,3,4,5) );

It returns this:

summing: 1 2 3 4 5, = -1.2289e+09
sum = -1.2289e+09


Looks like the "for @list" has one element in it, "1 2 3 4 5".

I'v tried:

say "sum = ", xsum( (1,2,3,4,5) );

and

say "sum = ", xsum( [1,2,3,4,5] );

and

my @x = (1,2,3,4,5);
say "sum = ", xsum( @x );

I'm using:
This is Rakudo Perl 6, revision 30434 built on parrot 0.7.0-devel

Thanks,

-Andy


Re: new method question

2008-08-22 Thread Moritz Lenz
Xiao Yafeng wrote:
> There are no barewords in Perl 6, but it seems new method is an exception:
> 
> class Dog {
> 
> has $name;

Attributes need to have a twigil, so it would be
  has $.name

> method bark () {
> say $name;
> }
> }
> my $p = Dog.new($name => 'boo');

You haven't defined a variable $name in this scope, so you can't use on.

>   $p.bark;#error!
> my $p = Dog.new( name => 'boo');
>   $p.bark#say boo

the => automatically quotes the left hand side, just like in perl 5. I
see no barewords here.

This works fine with current rakudo:

class Dog {
has $.name;
method bark() {
say $.name;
}
}

my $x = Dog.new(name => 'boo');
$x.bark;

> more confused:
> 
> class Dog {
> 
> has @names;
> method bark () {
> say @names;
> }
> }
> my $p = Dog.new(names => 'boo');
>   $p.bark;#nothing but passed.
> 
> So, how set array attribute of a class by new method?

I'd expect this to work:

class Dog {
has @.names;
method bark() {
say @.names.join(', ');
}
}

my $x = Dog.new(:names);
# or: Dog.new(name => ('foo', 'bar'))
$x.bark;

but currently it doesn't in rakudo (in does in pugs though)

-- 
Moritz Lenz
http://moritz.faui2k3.org/ |  http://perl-6.de/


Re: whats wrong with this code?

2008-08-22 Thread Moritz Lenz
Hi Andy,

you seem to have discovered a whole bunch of bugs at once :/


Andy Colson wrote:
> Hi List --
> 
> I'v started playing around with perl 6, and I am having problems with 
> this example:
> 
> use v6;
> 
> sub xsum (@list)
> {
>  my $i = 0;
>  print "summing: ";
>  for @list
>  {
>  $i += $_;
>  print $_,",";
>  }
>  say " = $i";
>  return $i;
> }
> say "sum = ", xsum( (1,2,3,4,5) );
> 
> It returns this:
> 
> summing: 1 2 3 4 5, = -1.2289e+09
> sum = -1.2289e+09
> 
> 
> Looks like the "for @list" has one element in it, "1 2 3 4 5".

Not quite. When you add these lines:
say @list.elems;
say @list.WHAT
at the start of the sub, you'll get 5 as the answer for the count, which
seems correct, and 'List' as the type, which seems weird, but I'm not
sure if it's actually wrong (normally something that starts with an @
should be an Array, but since it's a subroutine argument it's read only.
So List might be fine).

The 'for' in conjunction with this weird List is broken. This works:
my @list = (1, 2, 3, 4, 5); my @x = @list; for @x { .say }
(prints all numbers on separate lines)
but this is broken:
sub a(@b) { for @b { .say } }; my @x = (1, 2, 3); a(@x)
prints all at once.

The second problem is the result of the += operation, which does
something really weird.
When you write it as $i = $i + 1, it gives you 5, which is the number of
items in the Array $i (which is correct, if we accept the previous bug
for the moment)

I'll write bug reports for both problems to [EMAIL PROTECTED]



The recommended way to write such a sub is

sub xsum([EMAIL PROTECTED]) { ... }
xsum(1, 2, 3);

With the * before the @list it is "slurpy", which means that it doesn't
expect one list as the argument, but rather an arbitrary number of items.
If you happen to have an Array already, you can interpolate it:
my @x = 1, 2, 3;
xsum(|@x);


HTH,
Moritz

-- 
Moritz Lenz
http://moritz.faui2k3.org/ |  http://perl-6.de/


Re: whats wrong with this code?

2008-08-22 Thread Andy Colson

Moritz Lenz wrote:

Hi Andy,

you seem to have discovered a whole bunch of bugs at once :/


Andy Colson wrote:

Hi List --

I'v started playing around with perl 6, and I am having problems with 
this example:


use v6;

sub xsum (@list)
{
 my $i = 0;
 print "summing: ";
 for @list
 {
 $i += $_;
 print $_,",";
 }
 say " = $i";
 return $i;
}
say "sum = ", xsum( (1,2,3,4,5) );

It returns this:

summing: 1 2 3 4 5, = -1.2289e+09
sum = -1.2289e+09


Looks like the "for @list" has one element in it, "1 2 3 4 5".


Not quite. When you add these lines:
say @list.elems;
say @list.WHAT
at the start of the sub, you'll get 5 as the answer for the count, which
seems correct, and 'List' as the type, which seems weird, but I'm not
sure if it's actually wrong (normally something that starts with an @
should be an Array, but since it's a subroutine argument it's read only.
So List might be fine).

The 'for' in conjunction with this weird List is broken. This works:
my @list = (1, 2, 3, 4, 5); my @x = @list; for @x { .say }
(prints all numbers on separate lines)
but this is broken:
sub a(@b) { for @b { .say } }; my @x = (1, 2, 3); a(@x)
prints all at once.

The second problem is the result of the += operation, which does
something really weird.
When you write it as $i = $i + 1, it gives you 5, which is the number of
items in the Array $i (which is correct, if we accept the previous bug
for the moment)

I'll write bug reports for both problems to [EMAIL PROTECTED]



The recommended way to write such a sub is

sub xsum([EMAIL PROTECTED]) { ... }
xsum(1, 2, 3);

With the * before the @list it is "slurpy", which means that it doesn't
expect one list as the argument, but rather an arbitrary number of items.
If you happen to have an Array already, you can interpolate it:
my @x = 1, 2, 3;
xsum(|@x);


HTH,
Moritz



Cool, thanks for the help there.

> The recommended way to write such a sub is
>
> sub xsum([EMAIL PROTECTED]) { ... }
> xsum(|@x);

Ahh, but, if I already had a list, would that flatten and then rebuild 
the list, correct? (and would, effectively, make a copy of the list?) 
(vs. just passing the list?) (just passing a list like that passes a 
reference, right? and wont make a new copy?)


Thanks,

-Andy


Re: whats wrong with this code?

2008-08-22 Thread Patrick R. Michaud
On Fri, Aug 22, 2008 at 04:34:04PM -0500, Andy Colson wrote:
> sub xsum (@list)
> {
> my $i = 0;
> print "summing: ";
> for @list
> {
> $i += $_;
> print $_,",";
> }
> say " = $i";
> return $i;
> }
> say "sum = ", xsum( (1,2,3,4,5) );
>
> It returns this:
>
> summing: 1 2 3 4 5, = -1.2289e+09
> sum = -1.2289e+09

I suspect that Rakudo is having trouble binding array parameters
at the moment -- so it's likely a bug in the parameter handling code
(which I'm expecting will need some refactoring soon anyway).  I'm
guessing that Rakudo is binding @list as if it is a Scalar Array,
and thus the for loop sees only one element.

This probably deserves a tracking ticket at <[EMAIL PROTECTED]>.

Pm


Re: whats wrong with this code?

2008-08-22 Thread Moritz Lenz
Patrick R. Michaud wrote:
> On Fri, Aug 22, 2008 at 04:34:04PM -0500, Andy Colson wrote:
>> sub xsum (@list)
>> {
>> my $i = 0;
>> print "summing: ";
>> for @list
>> {
>> $i += $_;
>> print $_,",";
>> }
>> say " = $i";
>> return $i;
>> }
>> say "sum = ", xsum( (1,2,3,4,5) );
>>
>> It returns this:
>>
>> summing: 1 2 3 4 5, = -1.2289e+09
>> sum = -1.2289e+09
> 
> I suspect that Rakudo is having trouble binding array parameters
> at the moment -- so it's likely a bug in the parameter handling code
> (which I'm expecting will need some refactoring soon anyway).  I'm
> guessing that Rakudo is binding @list as if it is a Scalar Array,
> and thus the for loop sees only one element.
> 
> This probably deserves a tracking ticket at <[EMAIL PROTECTED]>.

Already wrote one:

[perl #58276] AutoReply: [BUG] Can't iterate over list that was passed
as a subroutine argument

Cheers,
Moritz

-- 
Moritz Lenz
http://moritz.faui2k3.org/ |  http://perl-6.de/


Re: whats wrong with this code?

2008-08-22 Thread Larry Wall
On Fri, Aug 22, 2008 at 05:30:19PM -0500, Andy Colson wrote:
> Moritz Lenz wrote:
> > The recommended way to write such a sub is
> >
> > sub xsum([EMAIL PROTECTED]) { ... }
> > xsum(|@x);
>
> Ahh, but, if I already had a list, would that flatten and then rebuild  
> the list, correct? (and would, effectively, make a copy of the list?)  
> (vs. just passing the list?) (just passing a list like that passes a  
> reference, right? and wont make a new copy?)

If bound to a list context (such as provided in this case by the
slurpy parameter), the | is relatively useless, since the @x would
flatten anyway.  In other words, there's little difference between
xsum(@x) and xsum(1,2,3,4,5) unless the signature tries to bind the
first argument to a scalar.  Whether one syntax or the other would
result in less work, or whether it'd compile down to the same code,
probably depends on how much the compiler guesses about the eventual
signature at compile time, so I couldn't guess which would be faster.
I do think xsum(@x) is probably clearer, especially if you're already
thinking of it as slurpy.  The use of | generally means you're trying
to cheat on the scalar parameters somehow.  It's not needed in list
context.

Larry


Re: new method question

2008-08-22 Thread John M. Dlugosz

Moritz Lenz moritz-at-casella.verplant.org |Perl 6| wrote:

Attributes need to have a twigil, so it would be
  has $.name

  


The syntax
   has $name;
with no twigil is legal according to S12.  Perhaps the original poster 
(Xiao Yafeng) might like to read 
.


--John