> So these are equivalent:
seq 10 | perl6 -ne 'my Int $y += $_; END { print $y; }'
seq 10 | perl6 -e '(my Int $y += $_; END { print $y; }) for lines'
> (Note that I needed to surround it in parentheses so that it is one
> statement.)
> It could be argued that -n should turn your code into a lambda first.
seq 10 | perl6 -e '{ my Int $y += $_; END { print $y; } } for lines'
10
Right, this is what I sort of expect from
seq 10 | perl6 -ne 'my Int $y += $_; END { print $y; }'
as the "my" would seem to localize $y in the while (or for) loop, so it
shouldn't accumulate. But it does.
> Then you would need to use the 「state」 keyword more often.
seq 10 | perl6 -e '{ state Int $y += $_; END { print $y; } } for lines'
55
The use of "state" makes sense. In p5
$ seq 10 | perl -nE ' my $y += $_} ; END { say $y; '
[crickets]
$ seq 10 | perl -nE ' $y += $_} ; { say $y; '
55
$ seq 10 | perl -nE ' our $y += $_} ; { say $y; '
55
Hmm:
$ seq 10 | perl -mstrict -wnE ' $y += $_} ; { say $y; '
55
$ seq 10 | perl -mstrict -wnE ' my $y += $_} ; { say $y; '
Name "main::y" used only once: possible typo at -e line 1.
Use of uninitialized value $y in say at -e line 1, <> line 10.
$ seq 10 | perl -mstrict -wnE ' our $y += $_} ; { say $y; '
55
I'd've thought the first one would get a warning too.
$ seq 10 | perl6 -ne 'our Int $y += $_; END { say $y; }'
===SORRY!=== Error while compiling -e
Cannot put a type constraint on an 'our'-scoped variable
at -e:1
------> our Int $y⏏ += $_; END { say $y; }
expecting any of:
constraint
________________________________
From: Brad Gilbert <[email protected]>
Sent: Thursday, September 26, 2019 9:52 PM
To: Andy Bach <[email protected]>
Cc: William Michels <[email protected]>; yary <[email protected]>; perl6
<[email protected]>; Joseph Brenner <[email protected]>; Elizabeth Mattijsen
<[email protected]>; Marc Chantreux <[email protected]>; Vittore Scolari
<[email protected]>
Subject: Re: anything faster than say [+] lines?
With the Perl5 compiler the -n flag literally adds this around your code before
compiling:
while ( <> ) {
…
}
Rakudo handles -n by transforming the AST (or the bytecode) into something that
loops.
Basically it is more like:
… for lines
(In that it doesn't affect scoping or compile-time effects.)
So these are equivalent:
seq 10 | perl6 -ne 'my Int $y += $_; END { print $y; }'
seq 10 | perl6 -e '(my Int $y += $_; END { print $y; }) for lines'
(Note that I needed to surround it in parentheses so that it is one statement.)
It could be argued that -n should turn your code into a lambda first.
seq 10 | perl6 -e '{ my Int $y += $_; END { print $y; } } for lines'
10
Then you would need to use the 「state」 keyword more often.
seq 10 | perl6 -e '{ state Int $y += $_; END { print $y; } } for lines'
55
On Thu, Sep 26, 2019 at 4:31 PM Andy Bach
<[email protected]<mailto:[email protected]>> wrote:
> Could the "-e" flag be limiting variable initializations to one?
I don't think so. I recall the -n being shorthand for wrapping your -e program
in
while ( <> ) {
# your program here
}
(-p just adds a continue "print" block, I believe), as folks would do cool
tricks of writing their -e script to have an early close while curly, instead
of, say, using END blocks
$ seq 10 | perl -nE ' $y += $_} ; { say $y; '
55
Note: using "my"
$ seq 10 | perl -nE ' my $y += $_} ; { say $y; '
[crickets]
gets you nothing, as $y is scoped to the -n while loop ;->
________________________________
From: William Michels <[email protected]<mailto:[email protected]>>
Sent: Thursday, September 26, 2019 3:01 PM
To: yary <[email protected]<mailto:[email protected]>>
Cc: perl6 <[email protected]<mailto:[email protected]>>; Andy Bach
<[email protected]<mailto:[email protected]>>; Joseph
Brenner <[email protected]<mailto:[email protected]>>; Elizabeth Mattijsen
<[email protected]<mailto:[email protected]>>; Marc Chantreux
<[email protected]<mailto:[email protected]>>; Vittore Scolari
<[email protected]<mailto:[email protected]>>
Subject: Re: anything faster than say [+] lines?
Hi Yary,
Honestly, I just tried re-writing the fastest StackOverflow answer
(written in Perl 5) that I found below, in Perl 6. To write P5 as P6 I
had to declare the variable $x with 'my'. Then I played around with a
declaration restricting to "Int" type (to look at potential
performance hits), just because well--with Perl 6--I could.
>#Perl 5 code:
>seq 1000000 | perl -lne '$x += $_; END { print $x; }'
https://stackoverflow.com/a/47162173
I'm guessing the answer as to 'why "my Int $y" isn't re-initialized
every time' in P6 is similar to the reason in P5? Could the "-e" flag
be limiting variable initializations to one?
Best Regards, Bill.
On Thu, Sep 26, 2019 at 12:00 PM yary
<[email protected]<mailto:[email protected]>> wrote:
>
> I see that Int/Num error, and also would like an explanation as to why "my
> Int $y" isn't re-initialized to Any each time through this loop
>
> $ seq 1000000 | perl6 -ne 'my Int $y += $_; END { print $y; }'
>
> Type check failed in assignment to $y; expected Int but got Num
> (500000500000e0)
>
> in block <unit> at -e line 1
>
>
> $ perl6 --version
>
> This is Rakudo Star version 2019.03.1 built on MoarVM version 2019.03
>
> implementing Perl 6.d.
>
>
> -y
>
>
> On Thu, Sep 26, 2019 at 2:24 PM William Michels via perl6-users
> <[email protected]<mailto:[email protected]>> wrote:
>>
>> Thank you, Andy and Joseph!
>>
>>
>> On Thu, Sep 26, 2019 at 8:47 AM Andy Bach
>> <[email protected]<mailto:[email protected]>> wrote:
>> >
>> > > Still, it's just "works for me":
>> >
>> > seq 1000000 | time perl6 -ne 'my $y += $_; END { print $y; }'
>> >
>> > I think that's still the wrong one - your missing the "Int"
>> > $ seq 1000000 | perl6 -ne 'my Int $y += $_; END { print $y; }'
>> > 500000500000
>> >
>> > though that works here, admittedly, my p6 is sort old
>> > This is Rakudo version 2018.03 built on MoarVM version 2018.03
>> > implementing Perl 6.c.
>> >
>> > I'm a little puzzled, I'd've thought the loop around the 'my Int $y' would
>> > redeclare a local $y each time. Instead it behaves like:
>> > $ time perl6 -e 'my Int $y = 0;for ( 1 .. 1000000) { $y += $_} ; say $y; '
>> >
>> > (which is signficantly faster ;-)
>> > 500000500000
>> > real 0m1.229s
>> > user 0m1.254s
>> > sys 0m0.040s
>> >
>> > )
>> > ________________________________
>> > From: Joseph Brenner <[email protected]<mailto:[email protected]>>
>> > Sent: Wednesday, September 25, 2019 11:13 PM
>> > To: William Michels <[email protected]<mailto:[email protected]>>
>> > Cc: Marc Chantreux <[email protected]<mailto:[email protected]>>; Vittore
>> > Scolari <[email protected]<mailto:[email protected]>>;
>> > Elizabeth Mattijsen <[email protected]<mailto:[email protected]>>; perl6
>> > <[email protected]<mailto:[email protected]>>
>> > Subject: Re: anything faster than say [+] lines?
>> >
>> > Oh, wait. I tried the wrong one-liner. Still, it's just "works for me":
>> >
>> > seq 1000000 | time perl6 -ne 'my $y += $_; END { print $y; }'
>> > 50000050000029.29user 0.06system 0:28.41elapsed 103%CPU
>> > (0avgtext+0avgdata 76196maxresident)k
>> > 63328inputs+0outputs (32major+15588minor)pagefaults 0swaps
>> >
>> >
>> >
>> > On 9/25/19, Joseph Brenner <[email protected]<mailto:[email protected]>>
>> > wrote:
>> > > I just gave that one-liner a try, but I didn't see that error:
>> > >
>> > >> seq 1000000 | time perl6 -e 'say [+] lines'
>> > > 500000500000
>> > > 28.70user 0.07system 0:28.29elapsed 101%CPU (0avgtext+0avgdata
>> > > 74188maxresident)k
>> > > 63424inputs+0outputs (32major+15409minor)pagefaults 0swaps
>> > >
>> > >
>> > > perl6 --version
>> > > This is Rakudo Star version 2019.03.1 built on MoarVM version 2019.03
>> > > implementing Perl 6.d.
>> > >
>> > > uname -a
>> > > Linux fandango 4.9.0-8-686 #1 SMP Debian 4.9.144-3 (2019-02-02) i686
>> > > GNU/Linux
>> > >
>> > >
>> > >
>> > > On 9/24/19, William Michels via perl6-users
>> > > <[email protected]<mailto:[email protected]>> wrote:
>> > >> I'm seeing a strange error. I started trying out Marc's original code,
>> > >> then tried to adapt some Perl5-type solutions from SO to see how they
>> > >> performed when re-written as Perl6. One thing I wanted to explicitly
>> > >> test was how restricting to an "Int" type affected performance.
>> > >>
>> > >> However, I found a surprising result: a sequence of one-million Ints
>> > >> throws an error, but a sequence of 999,999 Ints does not:
>> > >>
>> > >>> mbook:~ homedir$ seq 1000000 | time perl6 -e 'say [+] lines'
>> > >>> 500000500000
>> > >>> 4.81 real 4.86 user 0.20 sys
>> > >>> mbook:~ homedir$ seq 1000000 | time perl6 -ne 'my $y += $_; END { print
>> > >>> $y; }'
>> > >>> 500000500000 4.88 real 5.06 user 0.19 sys
>> > >>> mbook:~ homedir$ seq 1000000 | time perl6 -ne 'my Int $y += $_; END {
>> > >>> print $y; }'
>> > >>> Type check failed in assignment to $y; expected Int but got Num
>> > >>> (500000500000e0)
>> > >>> in block <unit> at -e line 1
>> > >>> 499999500000 4.77 real 4.97 user 0.19 sys
>> > >>> mbook:~ homedir$ seq 999999 | time perl6 -ne 'my Int $y += $_; END {
>> > >>> print
>> > >>> $y; }'
>> > >>> 499999500000 4.86 real 5.05 user 0.19 sys
>> > >>> mbook:~ homedir$ perl6 -v
>> > >>> This is Rakudo version 2019.07.1 built on MoarVM version 2019.07.1
>> > >>> implementing Perl 6.d.
>> > >>> mbook:~ homedir$
>> > >>
>> > >> Any comments or explanation appreciated,
>> > >>
>> > >> Best Regards, Bill.
>> > >>
>> > >>
>> > >>
>> > >>
>> > >> On Tue, Sep 24, 2019 at 1:59 AM Marc Chantreux
>> > >> <[email protected]<mailto:[email protected]>> wrote:
>> > >>>
>> > >>> hello,
>> > >>>
>> > >>> > > > > nice ... but when x is ~ 75440 (not always), there is a problem
>> > >>> > > > What is x here?
>> > >>> > > sorry. x is the arg of seq (number of lines).
>> > >>> > That never happens on my laptop
>> > >>>
>> > >>> well.. so it's a problem with my station. nevermind :)
>> > >>>
>> > >>> thanks again for helping
>> > >>> marc
>> > >>
>> > >