Re: what is going on here?

2009-01-13 Thread Patrick R. Michaud
On Mon, Jan 12, 2009 at 12:11:59PM +0300, Richard Hainsworth wrote:
> But
> my @ranking = %players.sort :{.value}; # white space before :, no ws after :
>
> generates a syntax error with rakudo.
> Is this a raduko bug, or am I not understanding your generic argument?

Rakudo doesn't recognize adverbial blocks yet (i.e., a colon followed 
by braces).

Pm


Re: what is going on here?

2009-01-12 Thread Richard Hainsworth

Larry Wall wrote:

On Sat, Jan 10, 2009 at 11:26:50PM +0300, Richard Hainsworth wrote:
  

More precisely, I dont understand the meaning of the ':' after '.sort'



It is turning the method call into a list operator, essentially.
It's not the so-called indirect object syntax, or it would be written:

my @ranking = sort %players: { .value };

But the idea is related, and has a similar effect.  If you're familiar
with Haskell, it's a bit like the $ operator in working like an opening
parenthesis that needs no closing parenthesis.

  

things are becoming clearer. I tried out the alternatives:
my @ranking = %players.sort({.value});
has the same effect as as
my @ranking = %players.sort: {.value};
as you indicated.

Yet another way to write the sort is

my @ranking = %placers.sort :{ .value };

  

But
my @ranking = %players.sort :{.value}; # white space before :, no ws after :

generates a syntax error with rakudo.
Is this a raduko bug, or am I not understanding your generic argument?

but in this case, the colon is introducing an adverb which modifies
the previous operator, which happens to be sort.  The difference
is that you can't continue the argument list after that, except
with more adverbs, whereas with the list operator form, the block
is merely the first argument:

my @ranking = %placers.sort: { .value }, $some, $extra, $args;
(Not that sort would know what to do with extra args, but we're talking
about generic syntax...)

Larry
  


Re: what is going on here?

2009-01-11 Thread Larry Wall
On Sat, Jan 10, 2009 at 11:26:50PM +0300, Richard Hainsworth wrote:
> More precisely, I dont understand the meaning of the ':' after '.sort'

It is turning the method call into a list operator, essentially.
It's not the so-called indirect object syntax, or it would be written:

my @ranking = sort %players: { .value };

But the idea is related, and has a similar effect.  If you're familiar
with Haskell, it's a bit like the $ operator in working like an opening
parenthesis that needs no closing parenthesis.

Yet another way to write the sort is

my @ranking = %placers.sort :{ .value };

but in this case, the colon is introducing an adverb which modifies
the previous operator, which happens to be sort.  The difference
is that you can't continue the argument list after that, except
with more adverbs, whereas with the list operator form, the block
is merely the first argument:

my @ranking = %placers.sort: { .value }, $some, $extra, $args;

(Not that sort would know what to do with extra args, but we're talking
about generic syntax...)

Larry


Re: what is going on here?

2009-01-11 Thread Moritz Lenz
Richard Hainsworth wrote:
> Could someone help me understand what is going on in the following snippet?
> 
> my %players = {'william'=>2, 'peter'=>3,'john'=>1,'mary'=>5};
> my @ranking = %players.sort: { .value };
> for @ranking {.say};
> 
> I cut and pasted from Patrick's blog on sorting and played around  to 
> get an array.
> But ... I dont understand what is being passed to @ranking.
> More precisely, I dont understand the meaning of the ':' after '.sort'

There are three syntaxes for method calls:

$obj.method;# no args
$obj.method($args);
$obj.method: $args;

The latter is used in the example.

> Where is this behaviour described?

S12, probably.

Cheers,
Moritz


Re: what is going on here?

2009-01-11 Thread Илья
Hi!

> More precisely, I dont understand the meaning of the ':' after '.sort'

see line 1825 of S03
C<< infix:<:>>>, the invocant maker
...

ack (or grep) ': {}'  in Spec dir can give a lot of examples.

ihrd


Re: what is going on here?

2009-01-11 Thread Hal Wigoda

the first line creates a hash,
the second line sorts the hash values into an array.
the third loops thru the array values printing one array member per line
On Jan 10, 2009, at 2:26 PM, Richard Hainsworth wrote:

Could someone help me understand what is going on in the following  
snippet?


my %players = {'william'=>2, 'peter'=>3,'john'=>1,'mary'=>5};
my @ranking = %players.sort: { .value };
for @ranking {.say};

I cut and pasted from Patrick's blog on sorting and played around   
to get an array.

But ... I dont understand what is being passed to @ranking.
More precisely, I dont understand the meaning of the ':' after '.sort'

Where is this behaviour described? I looked in WITCH under :
but couldnt really understand it :(




Re: [Fwd: [Fwd: Re: what is going on here?]]

2009-01-11 Thread Brandon S. Allbery KF8NH

On 2009 Jan 11, at 3:50, Richard Hainsworth wrote:

To be precise - why the ':' after the sort?

'%players.sort' calls the 'sort' method/sub on the hash '%players'.  
'{.value}' runs '.value' on $_ at some point. But when?


So once again, what is the ':' doing? How else could this code be  
written?



I think I have this right:  perl5 has indirect object syntax, which is  
ambiguous (the parse of a program can change by whether perl5 had  
previously seen a sub by that name).


In perl6, the colon is required to signify an indirect object, even if  
there are no following parameters.


--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allb...@kf8nh.com
system administrator [openafs,heimdal,too many hats] allb...@ece.cmu.edu
electrical and computer engineering, carnegie mellon universityKF8NH




[Fwd: [Fwd: Re: what is going on here?]]

2009-01-11 Thread Richard Hainsworth

thanks for the response, but i was really looking for a bit more detail.

To be precise - why the ':' after the sort?

'%players.sort' calls the 'sort' method/sub on the hash '%players'. 
'{.value}' runs '.value' on $_ at some point. But when?


So once again, what is the ':' doing? How else could this code be written?

Hal Wigoda wrote:

the first line creates a hash,
the second line sorts the hash values into an array.
the third loops thru the array values printing one array member per line
On Jan 10, 2009, at 2:26 PM, Richard Hainsworth wrote:

Could someone help me understand what is going on in the following 
snippet?


my %players = {'william'=>2, 'peter'=>3,'john'=>1,'mary'=>5};
my @ranking = %players.sort: { .value };
for @ranking {.say};

I cut and pasted from Patrick's blog on sorting and played around  to 
get an array.

But ... I dont understand what is being passed to @ranking.
More precisely, I dont understand the meaning of the ':' after '.sort'

Where is this behaviour described? I looked in WITCH under :
but couldnt really understand it :(