Are there assignment forms of the indexing postcircumfix operators?

2010-01-27 Thread Carl Mäsak
I was going to submit this as a Rakudo bug report, but I'm not sure
it's (a natural consequence of) spec, so I'll ask here on p6l instead.

I already know I can do all of the following:

 $a[42];
 $a.postcircumfix:<[ ]>(42);
 $a = $a.postcircumfix:<[ ]>(42);
 $a.=postcircumfix:<[ ]>(42);

But can I do this?

 $a.=[42];

In other words, would the .=[...] syntax unambiguously mean
"positionally index the LHS and assign the result back to it"?

Same question for prefix .=[...]. And same question, of course, for
the other postcircumfix indexing operators. And, tentatively, for
method call syntax as well, even though the semantics would be "invoke
the LHS with these paramters and assign the return value back to it".

Rakudo currently doesn't recognize this syntax.

$ perl6 -e 'my @a = ; my $a = @a; $a.=[2]; say $a'
Could not find non-existent sub !.=
[...]

(Same result for hash access, function calls, and the prefix forms.)

STD.pm doesn't seem to have a problem with it.

$ std 'my $a; $a.=[42]'
ok 0:01.19 2487m

$ std 'my $a; $a.={"foo"}'
ok 0:01.16 2487m

$ std 'my $a; $a.=()'
ok 0:01.11 2487m

$ std '.=[42]'
ok 0:01.10 2479m

$ std '.={"foo"}'
ok 0:01.04 2479m

$ std '.=()'
ok 0:01.01 2479m

I have not checked to see what it parses the syntax into, though.

// Carl


Counting characters

2010-01-27 Thread Carl Mäsak
How is "character counting" done in Perl 6?

In Perl 5, it is `scalar tr/CG//` if I want to count the number of Cs
plus the number of Gs in a string.

S05 describes tr/// in terms of the .trans function, a handsome but
very different beast. Specifically, it doesn't seem to have a "scalar
context", with which one could count things.


Re: Counting characters

2010-01-27 Thread Mark J. Reed
What does trans return in numeric (+) context?

On Wednesday, January 27, 2010, Carl Mäsak  wrote:
> How is "character counting" done in Perl 6?
>
> In Perl 5, it is `scalar tr/CG//` if I want to count the number of Cs
> plus the number of Gs in a string.
>
> S05 describes tr/// in terms of the .trans function, a handsome but
> very different beast. Specifically, it doesn't seem to have a "scalar
> context", with which one could count things.
>

-- 
Mark J. Reed 


Re: Counting characters

2010-01-27 Thread Carl Mäsak
Mark (>), Carl (>>):
>> S05 describes tr/// in terms of the .trans function, a handsome but
>> very different beast. Specifically, it doesn't seem to have a "scalar
>> context", with which one could count things.
>
> What does trans return in numeric (+) context?

As spec'd, it returns the numification of the string resulting from
the substitution, I guess.

// Carl


Re: Counting characters

2010-01-27 Thread Matthew Walton
On Wed, Jan 27, 2010 at 1:19 PM, Carl Mäsak  wrote:
> Mark (>), Carl (>>):
>>> S05 describes tr/// in terms of the .trans function, a handsome but
>>> very different beast. Specifically, it doesn't seem to have a "scalar
>>> context", with which one could count things.
>>
>> What does trans return in numeric (+) context?
>
> As spec'd, it returns the numification of the string resulting from
> the substitution, I guess.
>
> // Carl
>

$str.comb(/C|G/).join('').chars might do it. It's maybe not quite as elegant...

Matt


Fwd: Counting characters

2010-01-27 Thread Carl Mäsak
Hm, lost p6l along the way. Forwarding.

-- Forwarded message --
From: Carl Mäsak 
Date: Wed, Jan 27, 2010 at 2:31 PM
Subject: Re: Counting characters
To: Moritz Lenz 


Moritz (>), Carl (>>):
>> How is "character counting" done in Perl 6?
>>
>> In Perl 5, it is `scalar tr/CG//` if I want to count the number of Cs
>> plus the number of Gs in a string.
>
> +$str.comb(/<[GC]>/)
>
> Probably not as efficient as in Perl 5, though

Heh, the thought about .comb seemed to occur to a lot of people
(including me) at the same time. Thanks. :)

Recognizing a lone character class inside a literal regex in a comb
call whose result gets immediately numified sounds to me like an
excellent use case for a sufficiently cunning optimizer. :)

// Carl


Re: Counting characters

2010-01-27 Thread Larry Wall
On Wed, Jan 27, 2010 at 01:24:48PM +, Matthew Walton wrote:
: On Wed, Jan 27, 2010 at 1:19 PM, Carl Mäsak  wrote:
: > Mark (>), Carl (>>):
: >>> S05 describes tr/// in terms of the .trans function, a handsome but
: >>> very different beast. Specifically, it doesn't seem to have a "scalar
: >>> context", with which one could count things.
: >>
: >> What does trans return in numeric (+) context?
: >
: > As spec'd, it returns the numification of the string resulting from
: > the substitution, I guess.
: >
: > // Carl
: >
: 
: $str.comb(/C|G/).join('').chars might do it. It's maybe not quite as 
elegant...

Hmm, what might be more elegant?  Maybe something like...

[+] $str.comb.Bag;

Probably does too much work building the Bag though, unless it can be
lazy somehow.  But the point is that Bags are really just histograms
with a cute name.

Larry