Re: liens and :chomp question

2020-08-31 Thread ToddAndMargo via perl6-users

On 2020-08-30 08:16, yary wrote:
Looking up https://docs.raku.org/routine/lines shows a Table of Contents 
with


class Cool
(Cool) routine lines
class Supply
(Supply) method lines
class Str
(Str) routine lines
class IO::CatHandle
(IO::CatHandle) method lines
class IO::Path
(IO::Path) method lines
class IO::Handle
(IO::Handle) routine lines
class IO::Socket::INET
(IO::Socket::INET) method lines



Hi Yary,

I am not finding such a critter (table of contents).
On mine, you have to scroll though each of them.

It would be a wonder addition if the technical
writers would stick your list at the top and make
them links to each subsection.

-T


Re: Any other way to do this

2020-08-31 Thread William Michels via perl6-users
I like your script, Bruce. And you are quite correct--my code seems to
work just fine without ".words":

~$ raku -e 'say @*ARGS.grep(*.Rat).sum;'

Best, Bill.


On Mon, Aug 31, 2020 at 1:42 PM Bruce Gray  wrote:
>
> Bill,
>
> You were correct that `!=== 0` is redundant in the original code, because a 
> numeric will be checked for zero-versus-not-zero in Boolean context, and 
> because `==` and `===` should mean the same thing when comparing a bare 
> numeric value.
>
> In your latest version, I want to point out that `.words` should be 
> redundant, since the shell will break up the command-line arguments on 
> whitespace, before ever handing them to Raku.
> Your code gives the same answer without `.words`.
>
> Raku supports the procedural and functional (and OO, too!) paradigms very 
> well.
> Here is a different version, for readers who have not yet embraced the 
> map/grep mindset we have been showing off.
>
> my @nums;
> for @*ARGS -> $arg {
> push @nums, $arg if $arg.Rat;
> }
> say @nums.sum;
>
>
> --
> Hope this helps,
> Bruce Gray (Util of PerlMonks)
>
>
> > On Aug 31, 2020, at 3:02 PM, William Michels  wrote:
> >
> > Very nice, Bruce and Daniel!
> >
> > I continued to hack on Rahakrishnan's code, then realized I could try
> > using Bruce's grep() call as a filter:
> >
> > ~$ raku -e '@*ARGS.words.grep(*.Rat).sum.say;'  100 200 300 apples
> > 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
> > 1195.7876
> >
> > HTH, Bill.
> >
> > On Mon, Aug 31, 2020 at 12:23 PM  wrote:
> >>
> >> I like Bruce's Regex-based approach.
> >>
> >> Here's how I'd probably approach the problem:
> >>
> >> raku -e 'say [+] @*ARGS.grep(+*)' 0 100 200 300 apples 400oranges 2kilos
> >> 18.7876 500 grams14 10stars10 sun100moon 77
> >>
> >> August 31, 2020 2:28 PM, "Bruce Gray"  wrote:
> >>
> >>> my $is_a_number = / ^ \d+ [ '.' \d* ]? $ /;
> >>>
> >>> my $sum = @*ARGS.grep($is_a_number).sum;
> >>>
> >>> say $sum;
> >>>
> >>> —
> >>> Hope this helps,
> >>> Bruce Gray (Util of PerlMonks)
> >>>
>  On Aug 31, 2020, at 12:22 PM, William Michels via perl6-users 
>   wrote:
> 
>  I think it looks very good, Radhakrishnan! Presumably you are happy
>  with the sum 1195.7876?
> 
>  ~$ raku -e 'for @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 apples
>  400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
>  100
>  200
>  300
>  18.7876
>  500
>  77
> 
>  I'm still mulling over whether or not the "!=== 0" is essential. I
>  have yet to mess-up the command line arguments sufficiently to require
>  it, and throwing a zero onto the command line seems to be handled
>  gracefully.
> 
>  Anyone else want to chime in?
> 
>  Best, Bill.
> 
>  On Mon, Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman
>   wrote:
> > Please see the following script that checks for type and sums up only 
> > the numbers passed as
> > arguments to the script in the command line. I would be grateful if any 
> > improvement or furtherance
> > to this script is offered. Thank you.
> >
> > #
> > # sums the numbers given in command line arguments and prints
> > #
> > my $sum = 0;
> > for @*ARGS
> > {
> > $sum += $_.Rat if $_.Int // 0 !=== 0;
> > }
> > say $sum;
> >
> > #
> > # command line execution
> > # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 grams14 
> > 10stars10 sun100moon 77
> > #
>


Re: liens and :chomp question

2020-08-31 Thread ToddAndMargo via perl6-users

On 2020-08-30 08:16, yary wrote:

Expanding on how to read the docs & signature a bit, from Tobias

You confuse two methods that have the same name "lines". One of them,
which exists in the IO::Path class, has a :chomp argument. The other,
on IO::Handle does not.
   "path".IO.lines()       <-- calls lines on an IO::Path (supports
:chomp)
   "path".IO.open.lines()  <-- calls lines on an IO::Handle (does
not support :chomp)


Looking up https://docs.raku.org/routine/lines shows a Table of Contents 
with


class Cool
(Cool) routine lines
class Supply
(Supply) method lines
class Str
(Str) routine lines
class IO::CatHandle
(IO::CatHandle) method lines
class IO::Path
(IO::Path) method lines
class IO::Handle
(IO::Handle) routine lines
class IO::Socket::INET
(IO::Socket::INET) method lines


Lots of different "lines" methods. If I click on IO::Handle it jumps to
sub          lines( $what = $*ARGFILES, |c)
multi method lines( IO::Handle:D: $limit, :$close )
multi method lines( IO::Handle:D:         :$close )

which indeed has no named argument for "chomp"

As an aside-Reading between the lines (no pun intended), I deduce that 
the IO::Handle "lines" method is its own implementation so that it can 
read "lazily" as needed, and to support the "close" option.


And as another aside, it has a "sub" which shows that when "lines" is 
called with no arguments, it defaults to reading from $*ARGFILES.


So, back to the table of contents. How does one know which "lines" 
routine to look at?


Either by thinking & remembering --
"oh 'file'.IO.open returns an IO::Handle that's the one to read"
"Huh 'file'.IO returns a path object, and there's /'(IO::Path) method/ 
/lines'/ listed lets look at that, hmm it has /chomp/"


or by experiment!
 > 'example.txt'.IO.WHAT
(Path)
 > 'example.txt'.IO.open.WHAT
(Handle)

-y


Thank you!


Re: How to I modify what is a new line in lines?

2020-08-31 Thread ToddAndMargo via perl6-users

On 2020-08-31 18:54, William Michels via perl6-users wrote:

On Mon, Aug 31, 2020 at 6:07 PM ToddAndMargo via perl6-users
 wrote:


On 2020-08-31 17:21, yary wrote:

First part of my previous email on this thread! Re-read this bit


First, you were looking at the docs for Path's "lines", but you are
using a string "lines" and those docs say

multi method lines(Str:D: $limit, :$chomp = True)
multi method lines(Str:D: :$chomp = True)

Files get "nl-in" due to the special case of text files having various
line endings to tweak.

Strings already have "split" and "comb" for all the flexibility one may
need there, and what you're playing with is more naturally
dd $_ for $x.split("\t"); # "a","b", ... removes \t
dd $_ for $x.split(//); "a\t","b\t", 


and restating the above: "string".lines is different a different method
from "File.txt".IO.lines, which is also different from
"File.txt".IO.open.lines .  Yes that's confusing but there are reasons
for all that which make sense when one thinks about them a while.

So if you want to split a string, use split!

If you want to experiment a text file's line endings with "lines", do
that experiment with a file! Which was the 2nd part of my previous email


dd $_ for 'line0-10.txt'.IO.lines(:nl-in["i","\n"], :!chomp)[0..3];
"Li"
"ne 0\n"
"Li"
"ne 1\n"


-y


Hi Yary,

I think I am getting it.  I am confusing str with file.

In the following, LineTabs.txt is
"Line 1\tLine 2\tLine 3\tLine 4\t"

$ raku -e 'dd $_ for "LinesTabs.txt".IO.lines( :!chomp,
:nl-in["\t"])[1,2,0];'
"Line 1\t"
"Line 2\t"
"Line 0\t"

$ raku -e 'dd $_ for "LinesTabs.txt".IO.lines( :chomp,
:nl-in["\t"])[1,2,0];'
"Line 1"
"Line 2"
"Line 0"

I am having trouble wrapping my mind around `dd $_ for`.  What
exactly is going on?

-T


dd is the data-dumper function. Used for debugging. See:

https://docs.raku.org/programs/01-debugging#index-entry-dd

Below, you're calling dd on $_ , which is why you don't have/need a
call to print/put/say in that line of code. It's dd() that's giving
you output:

$ raku -e 'dd($_) for "LinesTabs.txt".IO.lines( :chomp, :nl-in["\t"])[1,2,0];'

HTH, Bill.



Hi Bill,

I get it now.  The "$_" is coming from "for" not the "dd".

raku -e 'dd($_) for "LinesTabs.txt".IO.lines( :chomp, :nl-in["\t"])[1,2,0];'

Is a reverse way of saying:

$ raku -e 'for "LinesTabs.txt".IO.lines( :chomp, :nl-in["\t"])[1,2,0] 
{dd $_;}'


"Line 1"
"Line 2"
"Line 0"


$ raku -e 'for "LinesTabs.txt".IO.lines(:!chomp, :nl-in["\t"])[1,2,0] 
{dd $_;}'


"Line 1\t"
"Line 2\t"
"Line 0\t"

Thank you!

:-)

-T

p.s. I almost never use the reverse method because my
eyes have to go back and forth to figure it out,
not left to right.  I do see the utility it it though.
Maybe when I get better at it.


Re: How to I modify what is a new line in lines?

2020-08-31 Thread William Michels via perl6-users
On Mon, Aug 31, 2020 at 6:07 PM ToddAndMargo via perl6-users
 wrote:
>
> On 2020-08-31 17:21, yary wrote:
> > First part of my previous email on this thread! Re-read this bit
> >
> >> First, you were looking at the docs for Path's "lines", but you are
> >> using a string "lines" and those docs say
> >>
> >> multi method lines(Str:D: $limit, :$chomp = True)
> >> multi method lines(Str:D: :$chomp = True)
> >>
> >> Files get "nl-in" due to the special case of text files having various
> >> line endings to tweak.
> >>
> >> Strings already have "split" and "comb" for all the flexibility one may
> >> need there, and what you're playing with is more naturally
> >> dd $_ for $x.split("\t"); # "a","b", ... removes \t
> >> dd $_ for $x.split(//); "a\t","b\t", 
> >
> > and restating the above: "string".lines is different a different method
> > from "File.txt".IO.lines, which is also different from
> > "File.txt".IO.open.lines .  Yes that's confusing but there are reasons
> > for all that which make sense when one thinks about them a while.
> >
> > So if you want to split a string, use split!
> >
> > If you want to experiment a text file's line endings with "lines", do
> > that experiment with a file! Which was the 2nd part of my previous email
> >
> >> dd $_ for 'line0-10.txt'.IO.lines(:nl-in["i","\n"], :!chomp)[0..3];
> >> "Li"
> >> "ne 0\n"
> >> "Li"
> >> "ne 1\n"
> >
> > -y
>
> Hi Yary,
>
> I think I am getting it.  I am confusing str with file.
>
> In the following, LineTabs.txt is
> "Line 1\tLine 2\tLine 3\tLine 4\t"
>
> $ raku -e 'dd $_ for "LinesTabs.txt".IO.lines( :!chomp,
> :nl-in["\t"])[1,2,0];'
> "Line 1\t"
> "Line 2\t"
> "Line 0\t"
>
> $ raku -e 'dd $_ for "LinesTabs.txt".IO.lines( :chomp,
> :nl-in["\t"])[1,2,0];'
> "Line 1"
> "Line 2"
> "Line 0"
>
> I am having trouble wrapping my mind around `dd $_ for`.  What
> exactly is going on?
>
> -T

dd is the data-dumper function. Used for debugging. See:

https://docs.raku.org/programs/01-debugging#index-entry-dd

Below, you're calling dd on $_ , which is why you don't have/need a
call to print/put/say in that line of code. It's dd() that's giving
you output:

$ raku -e 'dd($_) for "LinesTabs.txt".IO.lines( :chomp, :nl-in["\t"])[1,2,0];'

HTH, Bill.


Re: How to I modify what is a new line in lines?

2020-08-31 Thread ToddAndMargo via perl6-users

On 2020-08-31 17:21, yary wrote:

First part of my previous email on this thread! Re-read this bit

First, you were looking at the docs for Path's "lines", but you are 
using a string "lines" and those docs say


multi method lines(Str:D: $limit, :$chomp = True)
multi method lines(Str:D: :$chomp = True)

Files get "nl-in" due to the special case of text files having various 
line endings to tweak.


Strings already have "split" and "comb" for all the flexibility one may 
need there, and what you're playing with is more naturally

dd $_ for $x.split("\t"); # "a","b", ... removes \t
dd $_ for $x.split(//); "a\t","b\t", 


and restating the above: "string".lines is different a different method 
from "File.txt".IO.lines, which is also different from 
"File.txt".IO.open.lines .  Yes that's confusing but there are reasons 
for all that which make sense when one thinks about them a while.


So if you want to split a string, use split!

If you want to experiment a text file's line endings with "lines", do 
that experiment with a file! Which was the 2nd part of my previous email



dd $_ for 'line0-10.txt'.IO.lines(:nl-in["i","\n"], :!chomp)[0..3];
"Li"
"ne 0\n"
"Li"
"ne 1\n"


-y


Hi Yary,

I think I am getting it.  I am confusing str with file.

In the following, LineTabs.txt is
"Line 1\tLine 2\tLine 3\tLine 4\t"

$ raku -e 'dd $_ for "LinesTabs.txt".IO.lines( :!chomp, 
:nl-in["\t"])[1,2,0];'

"Line 1\t"
"Line 2\t"
"Line 0\t"

$ raku -e 'dd $_ for "LinesTabs.txt".IO.lines( :chomp, 
:nl-in["\t"])[1,2,0];'

"Line 1"
"Line 2"
"Line 0"

I am having trouble wrapping my mind around `dd $_ for`.  What
exactly is going on?

-T


Re: print particular lines question

2020-08-31 Thread yary
Nope $_ is the "default topic" if you want to use the jargon. It has a
name, the underscore character.

$ is a nameless variable, jargon is "anonymous scalar"

$_ is different specialness from $

-y


On Mon, Aug 31, 2020 at 5:13 PM ToddAndMargo via perl6-users <
perl6-us...@perl.org> wrote:

> On 2020-08-31 17:03, yary wrote:
> > anonymous variable
>
> Would be safe thinking it had the same properties as `$_`?
>


Re: How to I modify what is a new line in lines?

2020-08-31 Thread yary
First part of my previous email on this thread! Re-read this bit

> First, you were looking at the docs for Path's "lines", but you are
> using a string "lines" and those docs say
>
> multi method lines(Str:D: $limit, :$chomp = True)
> multi method lines(Str:D: :$chomp = True)
>
> Files get "nl-in" due to the special case of text files having various
> line endings to tweak.
>
> Strings already have "split" and "comb" for all the flexibility one may
> need there, and what you're playing with is more naturally
> dd $_ for $x.split("\t"); # "a","b", ... removes \t
> dd $_ for $x.split(//); "a\t","b\t", 

and restating the above: "string".lines is different a different method
from "File.txt".IO.lines, which is also different from
"File.txt".IO.open.lines .  Yes that's confusing but there are reasons for
all that which make sense when one thinks about them a while.

So if you want to split a string, use split!

If you want to experiment a text file's line endings with "lines", do that
experiment with a file! Which was the 2nd part of my previous email

> dd $_ for 'line0-10.txt'.IO.lines(:nl-in["i","\n"], :!chomp)[0..3];
> "Li"
> "ne 0\n"
> "Li"
> "ne 1\n"

-y


On Mon, Aug 31, 2020 at 5:12 PM ToddAndMargo via perl6-users <
perl6-us...@perl.org> wrote:

> On 2020-08-30 08:42, yary wrote:
> > You were close!
> >
> > First, you were looking at the docs for Path's "lines", but you are
> > using a string "lines" and those docs say
> >
> > multi method lines(Str:D: $limit, :$chomp = True)
> > multi method lines(Str:D: :$chomp = True)
> >
> > Files get "nl-in" due to the special case of text files having various
> > line endings to tweak.
> >
> > Strings already have "split" and "comb" for all the flexibility one may
> > need there, and what you're playing with is more naturally
> > dd $_ for $x.split("\t"); # "a","b", ... removes \t
> > dd $_ for $x.split(//); "a\t","b\t", 
> >
> > Now back to the Path lines, which DOES let you specify line endings
> >
> > methodlines(IO::Path:D::$chomp=True, :$enc='utf8', :$nl-in= ["\x0A",
> > "\r\n"], |c-->Seq:D)
> >
> > $ cat line0-10.txt
> > Line 0
> > Line 1
> > Line 2
> > ...
> >
> > let's pretend that the letter "i" is a line ending.
> > named arguments can be conveniently written colon pairs :-)
> >
> > my $nl-in="i"; dd $_ for 'line0-10.txt'.IO.lines(:$nl-in)[0..3];
> > "L"
> > "ne 0\nL"
> > "ne 1\nL"
> > "ne 2\nL"
> >
> > How about splitting on either "i" or "\n", and not chomping
> >
> > my $nl-in=("i","\n"); dd $_ for 'line0-10.txt'.IO.lines(:$nl-in,
> > :!chomp)[0..3];
> > "Li"
> > "ne 0\n"
> > "Li"
> > "ne 1\n"
> >
> > To put in line endings without having a variable of the same name as the
> > naed arg, use the full form of the colon pair
> > dd $_ for 'line0-10.txt'.IO.lines(:nl-in["i","\n"], :!chomp)[0..3];
> > "Li"
> > "ne 0\n"
> > "Li"
> > "ne 1\n"
> >
> >
> >
> > -y
>
> Hi Yary,
>
> Now what am I doing wrong?
>
> p6 'my $x="a\tb\tc\td\t"; dd $x; for $x.lines(:nl-in["\t", 0x09],
> :!chomp) {dd $_};'
>
> Str $x = "a\tb\tc\td\t"
>
> "a\tb\tc\td\t"
>
> :'(
> -T
>


Re: print particular lines question

2020-08-31 Thread ToddAndMargo via perl6-users

On 2020-08-31 17:03, yary wrote:

anonymous variable


Would be safe thinking it had the same properties as `$_`?


Re: How to I modify what is a new line in lines?

2020-08-31 Thread ToddAndMargo via perl6-users

On 2020-08-30 08:42, yary wrote:

You were close!

First, you were looking at the docs for Path's "lines", but you are 
using a string "lines" and those docs say


multi method lines(Str:D: $limit, :$chomp = True)
multi method lines(Str:D: :$chomp = True)

Files get "nl-in" due to the special case of text files having various 
line endings to tweak.


Strings already have "split" and "comb" for all the flexibility one may 
need there, and what you're playing with is more naturally

dd $_ for $x.split("\t"); # "a","b", ... removes \t
dd $_ for $x.split(//); "a\t","b\t", 

Now back to the Path lines, which DOES let you specify line endings

methodlines(IO::Path:D::$chomp=True, :$enc='utf8', :$nl-in= ["\x0A", 
"\r\n"], |c-->Seq:D)


$ cat line0-10.txt
Line 0
Line 1
Line 2
...

let's pretend that the letter "i" is a line ending.
named arguments can be conveniently written colon pairs :-)

my $nl-in="i"; dd $_ for 'line0-10.txt'.IO.lines(:$nl-in)[0..3];
"L"
"ne 0\nL"
"ne 1\nL"
"ne 2\nL"

How about splitting on either "i" or "\n", and not chomping

my $nl-in=("i","\n"); dd $_ for 'line0-10.txt'.IO.lines(:$nl-in, 
:!chomp)[0..3];

"Li"
"ne 0\n"
"Li"
"ne 1\n"

To put in line endings without having a variable of the same name as the 
naed arg, use the full form of the colon pair

dd $_ for 'line0-10.txt'.IO.lines(:nl-in["i","\n"], :!chomp)[0..3];
"Li"
"ne 0\n"
"Li"
"ne 1\n"



-y


Hi Yary,

Now what am I doing wrong?

p6 'my $x="a\tb\tc\td\t"; dd $x; for $x.lines(:nl-in["\t", 0x09], 
:!chomp) {dd $_};'


Str $x = "a\tb\tc\td\t"

"a\tb\tc\td\t"

:'(
-T


Re: print particular lines question

2020-08-31 Thread yary
I like this better for alpha counter

raku -e "for (1..4) { say (BEGIN $ = 'AAA')++ }"

with BEGIN, the assignment of AAA happens once. With the earlier ||= it
checks each time through the loop.
-y


On Mon, Aug 31, 2020 at 5:03 PM yary  wrote:

> Not even a reset- every time there's a $ by itself it is a new/different
> anonymous variable. So it is only useful where it is never referred to
> anywhere else.
>
> $ raku -e "for (1..4) { say $++, ' ,  ', ++$; say 'again- ',$;}"
>
> 0 ,  1
>
> again- (Any)
>
> 1 ,  2
>
> again- (Any)
>
> 2 ,  3
>
> again- (Any)
>
> 3 ,  4
>
> again- (Any)
>
> Hmm, how to make an alpha counter?
>
> $ raku -e "for (1..4) { say ($ ||= 'AAA')++ }"
>
> AAA
>
> AAB
>
> AAC
>
> AAD
>
> There is also anonymous @ and % but I don't have an example off the top of
> my head.
> -y
>
>
> On Mon, Aug 31, 2020 at 4:57 PM ToddAndMargo via perl6-users <
> perl6-us...@perl.org> wrote:
>
>> On 2020-08-31 16:53, ToddAndMargo via perl6-users wrote:
>> >>> On Mon, Aug 31, 2020, 4:20 PM ToddAndMargo via perl6-users
>> >>> mailto:perl6-us...@perl.org>> wrote:
>> >>>
>> >>> On 2020-08-31 05:53, Brian Duggan wrote:
>> >>>  > On Monday, August 24, Curt Tilmes wrote:
>> >>>  >> $ cat Lines.txt | raku -e '.say for lines()[3,2,5]'
>> >>>  >
>> >>>  > The -n flag is an option here too:
>> >>>  >
>> >>>  > raku -ne '.say if $++ == 3|2|5' Lines.txt
>> >>>  >
>> >>>  > Brian
>> >>>  >
>> >>>
>> >
>>  Hi Bill,
>> 
>>  Works beatifically! And no bash pipe!
>> 
>>  $ raku -ne '.say if $++ == 3|2|5' Lines.txt
>>  Line 2
>>  Line 3
>>  Line 5
>> 
>>  What is `$++`?
>> 
>>  -T
>> 
>> >
>> > On 2020-08-31 16:36, yary wrote:
>> >> $ by itself is an anonymous variable, putting ++ after starts it at 0
>> >> (hmm or nil?) and increments up.
>> >>
>> >> By putting the plus plus first, ++$, it will start at 1, thanks to
>> >> pre-increment versus post increment
>> >>
>> >
>> > Hi Yary,
>> >
>> > Excellent instructions!  It is a counter.   I found
>> > it over on
>> >
>> >  https://docs.raku.org/perl6.html
>> >
>> > with a search on `$++`.  But I had to pick it up
>> > from "context"
>> >
>> >
>> >
>> > $ p6 'my @x=<"a" "b" "c">; for @x -> $i { print $++," ", ++$, " ", $i,
>> > "\n";}'
>> > 0 1 "a"
>> > 1 2 "b"
>> > 2 3 "c"
>> >
>> > Question: does the counter restart after its use, or do
>> > I need to do it myself?
>> >
>> > -T
>> >
>>
>> To answer my own question.  It resets itself:
>>
>> $ p6 'my @x=<"a" "b" "c">; for @x -> $i { print $++, " ", ++$, " ", $i,
>> "\n" }; print "\n", $++, "\n";'
>> 0 1 "a"
>> 1 2 "b"
>> 2 3 "c"
>>
>> 0
>>
>>
>>
>> --
>> ~~
>> Computers are like air conditioners.
>> They malfunction when you open windows
>> ~~
>>
>


Re: print particular lines question

2020-08-31 Thread Aureliano Guedes
Depends where in your code the $++ is.
It may play as global or as local.

raku -e 'for 1..3 {say $++}; say $++'

On Mon, Aug 31, 2020 at 9:03 PM ToddAndMargo via perl6-users <
perl6-us...@perl.org> wrote:

>
> >  adn
>
> fixed
>


-- 
Aureliano Guedes
skype: aureliano.guedes
contato:  (11) 94292-6110
whatsapp +5511942926110


Re: print particular lines question

2020-08-31 Thread yary
Not even a reset- every time there's a $ by itself it is a new/different
anonymous variable. So it is only useful where it is never referred to
anywhere else.

$ raku -e "for (1..4) { say $++, ' ,  ', ++$; say 'again- ',$;}"

0 ,  1

again- (Any)

1 ,  2

again- (Any)

2 ,  3

again- (Any)

3 ,  4

again- (Any)

Hmm, how to make an alpha counter?

$ raku -e "for (1..4) { say ($ ||= 'AAA')++ }"

AAA

AAB

AAC

AAD

There is also anonymous @ and % but I don't have an example off the top of
my head.
-y


On Mon, Aug 31, 2020 at 4:57 PM ToddAndMargo via perl6-users <
perl6-us...@perl.org> wrote:

> On 2020-08-31 16:53, ToddAndMargo via perl6-users wrote:
> >>> On Mon, Aug 31, 2020, 4:20 PM ToddAndMargo via perl6-users
> >>> mailto:perl6-us...@perl.org>> wrote:
> >>>
> >>> On 2020-08-31 05:53, Brian Duggan wrote:
> >>>  > On Monday, August 24, Curt Tilmes wrote:
> >>>  >> $ cat Lines.txt | raku -e '.say for lines()[3,2,5]'
> >>>  >
> >>>  > The -n flag is an option here too:
> >>>  >
> >>>  > raku -ne '.say if $++ == 3|2|5' Lines.txt
> >>>  >
> >>>  > Brian
> >>>  >
> >>>
> >
>  Hi Bill,
> 
>  Works beatifically! And no bash pipe!
> 
>  $ raku -ne '.say if $++ == 3|2|5' Lines.txt
>  Line 2
>  Line 3
>  Line 5
> 
>  What is `$++`?
> 
>  -T
> 
> >
> > On 2020-08-31 16:36, yary wrote:
> >> $ by itself is an anonymous variable, putting ++ after starts it at 0
> >> (hmm or nil?) and increments up.
> >>
> >> By putting the plus plus first, ++$, it will start at 1, thanks to
> >> pre-increment versus post increment
> >>
> >
> > Hi Yary,
> >
> > Excellent instructions!  It is a counter.   I found
> > it over on
> >
> >  https://docs.raku.org/perl6.html
> >
> > with a search on `$++`.  But I had to pick it up
> > from "context"
> >
> >
> >
> > $ p6 'my @x=<"a" "b" "c">; for @x -> $i { print $++," ", ++$, " ", $i,
> > "\n";}'
> > 0 1 "a"
> > 1 2 "b"
> > 2 3 "c"
> >
> > Question: does the counter restart after its use, or do
> > I need to do it myself?
> >
> > -T
> >
>
> To answer my own question.  It resets itself:
>
> $ p6 'my @x=<"a" "b" "c">; for @x -> $i { print $++, " ", ++$, " ", $i,
> "\n" }; print "\n", $++, "\n";'
> 0 1 "a"
> 1 2 "b"
> 2 3 "c"
>
> 0
>
>
>
> --
> ~~
> Computers are like air conditioners.
> They malfunction when you open windows
> ~~
>


Re: print particular lines question

2020-08-31 Thread ToddAndMargo via perl6-users




 adn


fixed


Re: print particular lines question

2020-08-31 Thread ToddAndMargo via perl6-users

On 2020-08-31 16:57, ToddAndMargo via perl6-users wrote:

On 2020-08-31 16:53, ToddAndMargo via perl6-users wrote:
On Mon, Aug 31, 2020, 4:20 PM ToddAndMargo via perl6-users 
mailto:perl6-us...@perl.org>> wrote:


    On 2020-08-31 05:53, Brian Duggan wrote:
 > On Monday, August 24, Curt Tilmes wrote:
 >> $ cat Lines.txt | raku -e '.say for lines()[3,2,5]'
 >
 > The -n flag is an option here too:
 >
 > raku -ne '.say if $++ == 3|2|5' Lines.txt
 >
 > Brian
 >




    Hi Bill,

    Works beatifically! And no bash pipe!

    $ raku -ne '.say if $++ == 3|2|5' Lines.txt
    Line 2
    Line 3
    Line 5

    What is `$++`?

    -T



On 2020-08-31 16:36, yary wrote:
$ by itself is an anonymous variable, putting ++ after starts it at 0 
(hmm or nil?) and increments up.


By putting the plus plus first, ++$, it will start at 1, thanks to 
pre-increment versus post increment




Hi Yary,

Excellent instructions!  It is a counter.   I found
it over on

 https://docs.raku.org/perl6.html

with a search on `$++`.  But I had to pick it up
from "context"



$ p6 'my @x=<"a" "b" "c">; for @x -> $i { print $++," ", ++$, " ", $i, 
"\n";}'

0 1 "a"
1 2 "b"
2 3 "c"

Question: does the counter restart after its use, or do
I need to do it myself?

-T



To answer my own question.  It resets itself:

$ p6 'my @x=<"a" "b" "c">; for @x -> $i { print $++, " ", ++$, " ", $i, 
"\n" }; print "\n", $++, "\n";'

0 1 "a"
1 2 "b"
2 3 "c"

0





perl6.++.counters.txt

++ counters:

$++ adn ++$ are both anonymous variables

   `$++` is a counter that start at zero and increments by 1
   `++$` is a counter that start at one and increments by 1

and the reset themselves.

For example:

$ p6 'my @x=<"a" "b" "c">;
 for @x -> $i { print $++, " ", ++$, " ", $i, "\n" };
 print "\n", $++, "\n";'

0 1 "a"
1 2 "b"
2 3 "c"

0


Re: print particular lines question

2020-08-31 Thread ToddAndMargo via perl6-users

On 2020-08-31 16:53, ToddAndMargo via perl6-users wrote:
On Mon, Aug 31, 2020, 4:20 PM ToddAndMargo via perl6-users 
mailto:perl6-us...@perl.org>> wrote:


    On 2020-08-31 05:53, Brian Duggan wrote:
 > On Monday, August 24, Curt Tilmes wrote:
 >> $ cat Lines.txt | raku -e '.say for lines()[3,2,5]'
 >
 > The -n flag is an option here too:
 >
 > raku -ne '.say if $++ == 3|2|5' Lines.txt
 >
 > Brian
 >




    Hi Bill,

    Works beatifically! And no bash pipe!

    $ raku -ne '.say if $++ == 3|2|5' Lines.txt
    Line 2
    Line 3
    Line 5

    What is `$++`?

    -T



On 2020-08-31 16:36, yary wrote:
$ by itself is an anonymous variable, putting ++ after starts it at 0 
(hmm or nil?) and increments up.


By putting the plus plus first, ++$, it will start at 1, thanks to 
pre-increment versus post increment




Hi Yary,

Excellent instructions!  It is a counter.   I found
it over on

     https://docs.raku.org/perl6.html

with a search on `$++`.  But I had to pick it up
from "context"



$ p6 'my @x=<"a" "b" "c">; for @x -> $i { print $++," ", ++$, " ", $i, 
"\n";}'

0 1 "a"
1 2 "b"
2 3 "c"

Question: does the counter restart after its use, or do
I need to do it myself?

-T



To answer my own question.  It resets itself:

$ p6 'my @x=<"a" "b" "c">; for @x -> $i { print $++, " ", ++$, " ", $i, 
"\n" }; print "\n", $++, "\n";'

0 1 "a"
1 2 "b"
2 3 "c"

0



--
~~
Computers are like air conditioners.
They malfunction when you open windows
~~


Re: print particular lines question

2020-08-31 Thread ToddAndMargo via perl6-users
On Mon, Aug 31, 2020, 4:20 PM ToddAndMargo via perl6-users 
mailto:perl6-us...@perl.org>> wrote:


On 2020-08-31 05:53, Brian Duggan wrote:
 > On Monday, August 24, Curt Tilmes wrote:
 >> $ cat Lines.txt | raku -e '.say for lines()[3,2,5]'
 >
 > The -n flag is an option here too:
 >
 > raku -ne '.say if $++ == 3|2|5' Lines.txt
 >
 > Brian
 >




Hi Bill,

Works beatifically! And no bash pipe!

$ raku -ne '.say if $++ == 3|2|5' Lines.txt
Line 2
Line 3
Line 5

What is `$++`?

-T



On 2020-08-31 16:36, yary wrote:
$ by itself is an anonymous variable, putting ++ after starts it at 0 
(hmm or nil?) and increments up.


By putting the plus plus first, ++$, it will start at 1, thanks to 
pre-increment versus post increment




Hi Yary,

Excellent instructions!  It is a counter.   I found
it over on

https://docs.raku.org/perl6.html

with a search on `$++`.  But I had to pick it up
from "context"



$ p6 'my @x=<"a" "b" "c">; for @x -> $i { print $++," ", ++$, " ", $i, 
"\n";}'

0 1 "a"
1 2 "b"
2 3 "c"

Question: does the counter restart after its use, or do
I need to do it myself?

-T

--
~~
Computers are like air conditioners.
They malfunction when you open windows
~~


Re: print particular lines question

2020-08-31 Thread yary
Answering my own question, the operator sets the type of $. That's what
gradual typing is all about!

$ seq 5 | raku -ne "say $++"

0

1

2

3

4

$ seq 5 | raku -ne "say $ ~= 'Hi' "

Hi

HiHi

HiHiHi

HiHiHiHi

HiHiHiHiHi

$ seq 5 | raku -ne "say $++, $ ~= ' Hi' "

0 Hi

1 Hi Hi

2 Hi Hi Hi

3 Hi Hi Hi Hi

4 Hi Hi Hi Hi Hi

-y


On Mon, Aug 31, 2020 at 4:39 PM Aureliano Guedes 
wrote:

> Basically :
>
> $ raku -e 'my $a = 1; say ++$a; say $a'
> 2
> 2
> $ raku -e 'my $a = 1; say $a++; say $a'
> 1
> 2
>
> On Mon, Aug 31, 2020 at 8:36 PM yary  wrote:
>
>> $ by itself is an anonymous variable, putting ++ after starts it at 0
>> (hmm or nil?) and increments up.
>>
>> By putting the plus plus first, ++$, it will start at 1, thanks to
>> pre-increment versus post increment
>>
>> On Mon, Aug 31, 2020, 4:20 PM ToddAndMargo via perl6-users <
>> perl6-us...@perl.org> wrote:
>>
>>> On 2020-08-31 05:53, Brian Duggan wrote:
>>> > On Monday, August 24, Curt Tilmes wrote:
>>> >> $ cat Lines.txt | raku -e '.say for lines()[3,2,5]'
>>> >
>>> > The -n flag is an option here too:
>>> >
>>> > raku -ne '.say if $++ == 3|2|5' Lines.txt
>>> >
>>> > Brian
>>> >
>>>
>>> Hi Bill,
>>>
>>> Works beatifically! And no bash pipe!
>>>
>>> $ raku -ne '.say if $++ == 3|2|5' Lines.txt
>>> Line 2
>>> Line 3
>>> Line 5
>>>
>>> What is `$++`?
>>>
>>> -T
>>>
>>
>
> --
> Aureliano Guedes
> skype: aureliano.guedes
> contato:  (11) 94292-6110
> whatsapp +5511942926110
>


Re: print particular lines question

2020-08-31 Thread Aureliano Guedes
Basically :

$ raku -e 'my $a = 1; say ++$a; say $a'
2
2
$ raku -e 'my $a = 1; say $a++; say $a'
1
2

On Mon, Aug 31, 2020 at 8:36 PM yary  wrote:

> $ by itself is an anonymous variable, putting ++ after starts it at 0 (hmm
> or nil?) and increments up.
>
> By putting the plus plus first, ++$, it will start at 1, thanks to
> pre-increment versus post increment
>
> On Mon, Aug 31, 2020, 4:20 PM ToddAndMargo via perl6-users <
> perl6-us...@perl.org> wrote:
>
>> On 2020-08-31 05:53, Brian Duggan wrote:
>> > On Monday, August 24, Curt Tilmes wrote:
>> >> $ cat Lines.txt | raku -e '.say for lines()[3,2,5]'
>> >
>> > The -n flag is an option here too:
>> >
>> > raku -ne '.say if $++ == 3|2|5' Lines.txt
>> >
>> > Brian
>> >
>>
>> Hi Bill,
>>
>> Works beatifically! And no bash pipe!
>>
>> $ raku -ne '.say if $++ == 3|2|5' Lines.txt
>> Line 2
>> Line 3
>> Line 5
>>
>> What is `$++`?
>>
>> -T
>>
>

-- 
Aureliano Guedes
skype: aureliano.guedes
contato:  (11) 94292-6110
whatsapp +5511942926110


Re: print particular lines question

2020-08-31 Thread yary
$ by itself is an anonymous variable, putting ++ after starts it at 0 (hmm
or nil?) and increments up.

By putting the plus plus first, ++$, it will start at 1, thanks to
pre-increment versus post increment

On Mon, Aug 31, 2020, 4:20 PM ToddAndMargo via perl6-users <
perl6-us...@perl.org> wrote:

> On 2020-08-31 05:53, Brian Duggan wrote:
> > On Monday, August 24, Curt Tilmes wrote:
> >> $ cat Lines.txt | raku -e '.say for lines()[3,2,5]'
> >
> > The -n flag is an option here too:
> >
> > raku -ne '.say if $++ == 3|2|5' Lines.txt
> >
> > Brian
> >
>
> Hi Bill,
>
> Works beatifically! And no bash pipe!
>
> $ raku -ne '.say if $++ == 3|2|5' Lines.txt
> Line 2
> Line 3
> Line 5
>
> What is `$++`?
>
> -T
>


Re: print particular lines question

2020-08-31 Thread ToddAndMargo via perl6-users

On 2020-08-31 05:53, Brian Duggan wrote:

On Monday, August 24, Curt Tilmes wrote:

$ cat Lines.txt | raku -e '.say for lines()[3,2,5]'


The -n flag is an option here too:

raku -ne '.say if $++ == 3|2|5' Lines.txt

Brian



Hi Bill,

Works beatifically! And no bash pipe!

$ raku -ne '.say if $++ == 3|2|5' Lines.txt
Line 2
Line 3
Line 5

What is `$++`?

-T


Re: Any other way to do this

2020-08-31 Thread Bruce Gray
Bill,

You were correct that `!=== 0` is redundant in the original code, because a 
numeric will be checked for zero-versus-not-zero in Boolean context, and 
because `==` and `===` should mean the same thing when comparing a bare numeric 
value.

In your latest version, I want to point out that `.words` should be redundant, 
since the shell will break up the command-line arguments on whitespace, before 
ever handing them to Raku.
Your code gives the same answer without `.words`.

Raku supports the procedural and functional (and OO, too!) paradigms very well.
Here is a different version, for readers who have not yet embraced the map/grep 
mindset we have been showing off.

my @nums;
for @*ARGS -> $arg {
push @nums, $arg if $arg.Rat;
}
say @nums.sum;


-- 
Hope this helps,
Bruce Gray (Util of PerlMonks)


> On Aug 31, 2020, at 3:02 PM, William Michels  wrote:
> 
> Very nice, Bruce and Daniel!
> 
> I continued to hack on Rahakrishnan's code, then realized I could try
> using Bruce's grep() call as a filter:
> 
> ~$ raku -e '@*ARGS.words.grep(*.Rat).sum.say;'  100 200 300 apples
> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
> 1195.7876
> 
> HTH, Bill.
> 
> On Mon, Aug 31, 2020 at 12:23 PM  wrote:
>> 
>> I like Bruce's Regex-based approach.
>> 
>> Here's how I'd probably approach the problem:
>> 
>> raku -e 'say [+] @*ARGS.grep(+*)' 0 100 200 300 apples 400oranges 2kilos
>> 18.7876 500 grams14 10stars10 sun100moon 77
>> 
>> August 31, 2020 2:28 PM, "Bruce Gray"  wrote:
>> 
>>> my $is_a_number = / ^ \d+ [ '.' \d* ]? $ /;
>>> 
>>> my $sum = @*ARGS.grep($is_a_number).sum;
>>> 
>>> say $sum;
>>> 
>>> —
>>> Hope this helps,
>>> Bruce Gray (Util of PerlMonks)
>>> 
 On Aug 31, 2020, at 12:22 PM, William Michels via perl6-users 
  wrote:
 
 I think it looks very good, Radhakrishnan! Presumably you are happy
 with the sum 1195.7876?
 
 ~$ raku -e 'for @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 apples
 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
 100
 200
 300
 18.7876
 500
 77
 
 I'm still mulling over whether or not the "!=== 0" is essential. I
 have yet to mess-up the command line arguments sufficiently to require
 it, and throwing a zero onto the command line seems to be handled
 gracefully.
 
 Anyone else want to chime in?
 
 Best, Bill.
 
 On Mon, Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman
  wrote:
> Please see the following script that checks for type and sums up only the 
> numbers passed as
> arguments to the script in the command line. I would be grateful if any 
> improvement or furtherance
> to this script is offered. Thank you.
> 
> #
> # sums the numbers given in command line arguments and prints
> #
> my $sum = 0;
> for @*ARGS
> {
> $sum += $_.Rat if $_.Int // 0 !=== 0;
> }
> say $sum;
> 
> #
> # command line execution
> # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 grams14 
> 10stars10 sun100moon 77
> #


Re: Any other way to do this

2020-08-31 Thread William Michels via perl6-users
Sorry Radhakrishnan, for mis-spelling your name in my last post. --B.



On Mon, Aug 31, 2020 at 1:02 PM William Michels  wrote:
>
> Very nice, Bruce and Daniel!
>
> I continued to hack on Rahakrishnan's code, then realized I could try
> using Bruce's grep() call as a filter:
>
> ~$ raku -e '@*ARGS.words.grep(*.Rat).sum.say;'  100 200 300 apples
> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
> 1195.7876
>
> HTH, Bill.
>
> On Mon, Aug 31, 2020 at 12:23 PM  wrote:
> >
> > I like Bruce's Regex-based approach.
> >
> > Here's how I'd probably approach the problem:
> >
> > raku -e 'say [+] @*ARGS.grep(+*)' 0 100 200 300 apples 400oranges 2kilos
> > 18.7876 500 grams14 10stars10 sun100moon 77
> >
> > August 31, 2020 2:28 PM, "Bruce Gray"  wrote:
> >
> > > my $is_a_number = / ^ \d+ [ '.' \d* ]? $ /;
> > >
> > > my $sum = @*ARGS.grep($is_a_number).sum;
> > >
> > > say $sum;
> > >
> > > —
> > > Hope this helps,
> > > Bruce Gray (Util of PerlMonks)
> > >
> > >> On Aug 31, 2020, at 12:22 PM, William Michels via perl6-users 
> > >>  wrote:
> > >>
> > >> I think it looks very good, Radhakrishnan! Presumably you are happy
> > >> with the sum 1195.7876?
> > >>
> > >> ~$ raku -e 'for @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 apples
> > >> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
> > >> 100
> > >> 200
> > >> 300
> > >> 18.7876
> > >> 500
> > >> 77
> > >>
> > >> I'm still mulling over whether or not the "!=== 0" is essential. I
> > >> have yet to mess-up the command line arguments sufficiently to require
> > >> it, and throwing a zero onto the command line seems to be handled
> > >> gracefully.
> > >>
> > >> Anyone else want to chime in?
> > >>
> > >> Best, Bill.
> > >>
> > >> On Mon, Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman
> > >>  wrote:
> > >>> Please see the following script that checks for type and sums up only 
> > >>> the numbers passed as
> > >>> arguments to the script in the command line. I would be grateful if any 
> > >>> improvement or furtherance
> > >>> to this script is offered. Thank you.
> > >>>
> > >>> #
> > >>> # sums the numbers given in command line arguments and prints
> > >>> #
> > >>> my $sum = 0;
> > >>> for @*ARGS
> > >>> {
> > >>> $sum += $_.Rat if $_.Int // 0 !=== 0;
> > >>> }
> > >>> say $sum;
> > >>>
> > >>> #
> > >>> # command line execution
> > >>> # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 grams14 
> > >>> 10stars10 sun100moon 77
> > >>> #


Re: Any other way to do this

2020-08-31 Thread Bruce Gray
Daniel’s solution taught me to not ignore the simple numeric coercion operator: 
`+`.
I was trying to improve version just now, using `0 + $_` in a grep, and got 
hung up on the error thrown on failed numeric conversion.
I ended up with:
.grep: { try 0 + $_ };
Which does work, and might even be better than .grep(+*) , but I totally missed 
the coercion as a shortcut.

A few other thoughts:
* The original code fails to handle `0.01`, because (0.01).Int is 
exactly Zero, which fails the `!=== 0` before .Rat is ever called.
* My Regex fails to handle negative numbers, just due to my oversight. 
It also does not handle 0.3 expressed without the leading zero: `.3`
* All the non-regex solutions (including the original) have unexpected 
effects due to the use of Raku’s numeric conversion; none of these are rejected:
0xDEADBEEF
1e15
0b11
-0o77
15/60
* If the job of the code is “# sums the numbers…”, then we might need a 
clearer definition of Number.
Is it OK to accept whatever Raku can understand as a number?
If not, then a stricter filter must be implemented.

I finally settled on using `try` instead of numeric coercion, because if I am 
not golfing, I think `try` makes the grep look more like “filter out the 
non-numbers” instead of “get rid of the zero values”.
Also, I am using a `sub MAIN` with a comment that binds to `WHY` to give a 
better command-line usage message, and using named intermediate variables to 
clarify what we are trying to achieve.


#|(Print the sum of the command-line arguments, but only those arguments that 
are valid numbers)
sub MAIN ( *@possible_numbers ) {
die $*USAGE if not @possible_numbers;

my @actual_numbers = @possible_numbers.grep: { try 0 + $_ };

say @actual_numbers.sum;
}


Calling with a `—help` flag, or without arguments, now gives:
$ raku ~/T/d20200831/ee25_e.raku --help
Usage:
  /Users/bruce_pro/T/d20200831/ee25_e.raku [ ...] -- Print 
the sum of the command-line arguments, but only those arguments that are valid 
numbers

— 
Hope this helps,
Bruce Gray (Util of PerlMonks)

> On Aug 31, 2020, at 2:23 PM, dan...@codesections.com wrote:
> 
> I like Bruce's Regex-based approach.
> 
> Here's how I'd probably approach the problem:
> 
> raku -e 'say [+] @*ARGS.grep(+*)' 0 100 200 300 apples 400oranges 2kilos
> 18.7876 500 grams14 10stars10 sun100moon 77
> 
> August 31, 2020 2:28 PM, "Bruce Gray"  wrote:
> 
>> my $is_a_number = / ^ \d+ [ '.' \d* ]? $ /;
>> 
>> my $sum = @*ARGS.grep($is_a_number).sum;
>> 
>> say $sum;
>> 
>> — 
>> Hope this helps,
>> Bruce Gray (Util of PerlMonks)
>> 
>>> On Aug 31, 2020, at 12:22 PM, William Michels via perl6-users 
>>>  wrote:
>>> 
>>> I think it looks very good, Radhakrishnan! Presumably you are happy
>>> with the sum 1195.7876?
>>> 
>>> ~$ raku -e 'for @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 apples
>>> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
>>> 100
>>> 200
>>> 300
>>> 18.7876
>>> 500
>>> 77
>>> 
>>> I'm still mulling over whether or not the "!=== 0" is essential. I
>>> have yet to mess-up the command line arguments sufficiently to require
>>> it, and throwing a zero onto the command line seems to be handled
>>> gracefully.
>>> 
>>> Anyone else want to chime in?
>>> 
>>> Best, Bill.
>>> 
>>> On Mon, Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman
>>>  wrote:
 Please see the following script that checks for type and sums up only the 
 numbers passed as
 arguments to the script in the command line. I would be grateful if any 
 improvement or furtherance
 to this script is offered. Thank you.
 
 #
 # sums the numbers given in command line arguments and prints
 #
 my $sum = 0;
 for @*ARGS
 {
 $sum += $_.Rat if $_.Int // 0 !=== 0;
 }
 say $sum;
 
 #
 # command line execution
 # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 grams14 
 10stars10 sun100moon 77
 #


Re: Any other way to do this

2020-08-31 Thread William Michels via perl6-users
Very nice, Bruce and Daniel!

I continued to hack on Rahakrishnan's code, then realized I could try
using Bruce's grep() call as a filter:

~$ raku -e '@*ARGS.words.grep(*.Rat).sum.say;'  100 200 300 apples
400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
1195.7876

HTH, Bill.

On Mon, Aug 31, 2020 at 12:23 PM  wrote:
>
> I like Bruce's Regex-based approach.
>
> Here's how I'd probably approach the problem:
>
> raku -e 'say [+] @*ARGS.grep(+*)' 0 100 200 300 apples 400oranges 2kilos
> 18.7876 500 grams14 10stars10 sun100moon 77
>
> August 31, 2020 2:28 PM, "Bruce Gray"  wrote:
>
> > my $is_a_number = / ^ \d+ [ '.' \d* ]? $ /;
> >
> > my $sum = @*ARGS.grep($is_a_number).sum;
> >
> > say $sum;
> >
> > —
> > Hope this helps,
> > Bruce Gray (Util of PerlMonks)
> >
> >> On Aug 31, 2020, at 12:22 PM, William Michels via perl6-users 
> >>  wrote:
> >>
> >> I think it looks very good, Radhakrishnan! Presumably you are happy
> >> with the sum 1195.7876?
> >>
> >> ~$ raku -e 'for @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 apples
> >> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
> >> 100
> >> 200
> >> 300
> >> 18.7876
> >> 500
> >> 77
> >>
> >> I'm still mulling over whether or not the "!=== 0" is essential. I
> >> have yet to mess-up the command line arguments sufficiently to require
> >> it, and throwing a zero onto the command line seems to be handled
> >> gracefully.
> >>
> >> Anyone else want to chime in?
> >>
> >> Best, Bill.
> >>
> >> On Mon, Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman
> >>  wrote:
> >>> Please see the following script that checks for type and sums up only the 
> >>> numbers passed as
> >>> arguments to the script in the command line. I would be grateful if any 
> >>> improvement or furtherance
> >>> to this script is offered. Thank you.
> >>>
> >>> #
> >>> # sums the numbers given in command line arguments and prints
> >>> #
> >>> my $sum = 0;
> >>> for @*ARGS
> >>> {
> >>> $sum += $_.Rat if $_.Int // 0 !=== 0;
> >>> }
> >>> say $sum;
> >>>
> >>> #
> >>> # command line execution
> >>> # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 grams14 
> >>> 10stars10 sun100moon 77
> >>> #


Re: Any other way to do this

2020-08-31 Thread daniel
I like Bruce's Regex-based approach.

Here's how I'd probably approach the problem:

raku -e 'say [+] @*ARGS.grep(+*)' 0 100 200 300 apples 400oranges 2kilos
18.7876 500 grams14 10stars10 sun100moon 77

August 31, 2020 2:28 PM, "Bruce Gray"  wrote:

> my $is_a_number = / ^ \d+ [ '.' \d* ]? $ /;
> 
> my $sum = @*ARGS.grep($is_a_number).sum;
> 
> say $sum;
> 
> — 
> Hope this helps,
> Bruce Gray (Util of PerlMonks)
> 
>> On Aug 31, 2020, at 12:22 PM, William Michels via perl6-users 
>>  wrote:
>> 
>> I think it looks very good, Radhakrishnan! Presumably you are happy
>> with the sum 1195.7876?
>> 
>> ~$ raku -e 'for @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 apples
>> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
>> 100
>> 200
>> 300
>> 18.7876
>> 500
>> 77
>> 
>> I'm still mulling over whether or not the "!=== 0" is essential. I
>> have yet to mess-up the command line arguments sufficiently to require
>> it, and throwing a zero onto the command line seems to be handled
>> gracefully.
>> 
>> Anyone else want to chime in?
>> 
>> Best, Bill.
>> 
>> On Mon, Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman
>>  wrote:
>>> Please see the following script that checks for type and sums up only the 
>>> numbers passed as
>>> arguments to the script in the command line. I would be grateful if any 
>>> improvement or furtherance
>>> to this script is offered. Thank you.
>>> 
>>> #
>>> # sums the numbers given in command line arguments and prints
>>> #
>>> my $sum = 0;
>>> for @*ARGS
>>> {
>>> $sum += $_.Rat if $_.Int // 0 !=== 0;
>>> }
>>> say $sum;
>>> 
>>> #
>>> # command line execution
>>> # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 grams14 
>>> 10stars10 sun100moon 77
>>> #


Re: Any other way to do this

2020-08-31 Thread Bruce Gray
my $is_a_number = / ^ \d+ [ '.' \d* ]? $ /;

my $sum = @*ARGS.grep($is_a_number).sum;

say $sum;

— 
Hope this helps,
Bruce Gray (Util of PerlMonks)

> On Aug 31, 2020, at 12:22 PM, William Michels via perl6-users 
>  wrote:
> 
> I think it looks very good, Radhakrishnan! Presumably you are happy
> with the sum 1195.7876?
> 
> ~$ raku -e 'for @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 apples
> 400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
> 100
> 200
> 300
> 18.7876
> 500
> 77
> 
> I'm still mulling over whether or not the "!=== 0" is essential.  I
> have yet to mess-up the command line arguments sufficiently to require
> it, and throwing a zero onto the command line seems to be handled
> gracefully.
> 
> Anyone else want to chime in?
> 
> Best, Bill.
> 
> On Mon, Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman
>  wrote:
>> 
>> Please see the following script that checks for type and sums up only the 
>> numbers passed as arguments to the script in the command line.  I would be 
>> grateful if any improvement or furtherance to this script is offered.  Thank 
>> you.
>> 
>> #
>> # sums the numbers given in command line arguments and prints
>> #
>> my $sum = 0;
>> for @*ARGS
>> {
>> $sum += $_.Rat if $_.Int // 0 !=== 0;
>> }
>> say $sum;
>> 
>> #
>> # command line execution
>> # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 grams14 
>> 10stars10 sun100moon 77
>> #


Re: Any other way to do this

2020-08-31 Thread William Michels via perl6-users
I think it looks very good, Radhakrishnan! Presumably you are happy
with the sum 1195.7876?

~$ raku -e 'for @*ARGS {.say if ($_.Int // 0) };' 0 100 200 300 apples
400oranges 2kilos 18.7876 500 grams14 10stars10 sun100moon 77
100
200
300
18.7876
500
77

I'm still mulling over whether or not the "!=== 0" is essential.  I
have yet to mess-up the command line arguments sufficiently to require
it, and throwing a zero onto the command line seems to be handled
gracefully.

Anyone else want to chime in?

Best, Bill.

On Mon, Aug 31, 2020 at 8:49 AM Radhakrishnan Venkataraman
 wrote:
>
> Please see the following script that checks for type and sums up only the 
> numbers passed as arguments to the script in the command line.  I would be 
> grateful if any improvement or furtherance to this script is offered.  Thank 
> you.
>
> #
> # sums the numbers given in command line arguments and prints
> #
> my $sum = 0;
> for @*ARGS
> {
> $sum += $_.Rat if $_.Int // 0 !=== 0;
> }
> say $sum;
>
> #
> # command line execution
> # perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 grams14 
> 10stars10 sun100moon 77
> #


Re: print particular lines question

2020-08-31 Thread Andy Bach
> Not getting back line #11 with
 perl -ne 'print if $. =~ /\b[3 2 5 11]\b/' test_lines.txt

Right, as the char class contains , 1, 2, 3 and 5. I guess alternatives
 perl -ne 'print if $. =~ /\b(1|5|3|11)\b/' /tmp/lines.txt
line: 1
line: 3
line: 5
line: 11




From: William Michels 
Sent: Monday, August 31, 2020 10:28 AM
To: Brian Duggan 
Cc: Andy Bach ; perl6-users 
Subject: Re: print particular lines question

How would P5 handle line numbers > 10 ? Not getting back line #11 with
the P5 examples below:

$ raku -ne '.say if ++$ == 3|2|5|11' test_lines.txt
Line 2
Line 3
Line 5
Line 11

~$ perl -ne 'print if $. =~ /\b[3 2 5 11]\b/' test_lines.txt
Line 1
Line 2
Line 3
Line 5

~$ perl -ne 'print if $. =~ /\b[3,2, 5, 11]\b/' test_lines.txt
Line 1
Line 2
Line 3
Line 5

On Mon, Aug 31, 2020 at 8:17 AM Brian Duggan  wrote:
>
> On Monday, August 31, Andy Bach wrote:
> > >  raku -ne '.say if $++ == 3|2|5' Lines.txt
> >
> > OT, maybe, but is
> > perl -ne 'print if $. =~ /\b[325]\b/' Lines.txt
> >
> > or
> > perl -ne 'print if $c++ =~ /\b[436]\b/' Lines.txt
> >
> > the best you can do in P5?
>
> I can't think of anything better :-)
>
> Brian


Any other way to do this

2020-08-31 Thread Radhakrishnan Venkataraman
Please see the following script that checks for type and sums up only the
numbers passed as arguments to the script in the command line.  I would be
grateful if any improvement or furtherance to this script is offered.
Thank you.

#
# sums the numbers given in command line arguments and prints
#
my $sum = 0;
for @*ARGS
{
$sum += $_.Rat if $_.Int // 0 !=== 0;
}
say $sum;

#
# command line execution
# perl6 cla.p6 100 200 300 apples 400oranges 2kilos 18.7876 500 grams14
10stars10 sun100moon 77
#


Re: print particular lines question

2020-08-31 Thread William Michels via perl6-users
Thanks Yary! So that means Brian's answer in Raku can use the
smartmatch operator instead of the "==". Good to know!

~$ raku -ne '.say  if ++$ ~~ 3|5|11' test_lines.txt
Line 3
Line 5
Line 11
On Mon, Aug 31, 2020 at 8:47 AM yary  wrote:
>
> Aww don't you remember Raku's earliest(?) contribution to Perl? I was so 
> happy when this arrived, and sad over its subsequent neglect
>
> perl -ne 'no warnings "experimental"; print if $. ~~ [3,5,11]' line0-10.txt
>
>
> -y
>
>
> On Mon, Aug 31, 2020 at 8:28 AM William Michels via perl6-users 
>  wrote:
>>
>> How would P5 handle line numbers > 10 ? Not getting back line #11 with
>> the P5 examples below:
>>
>> $ raku -ne '.say if ++$ == 3|2|5|11' test_lines.txt
>> Line 2
>> Line 3
>> Line 5
>> Line 11
>>
>> ~$ perl -ne 'print if $. =~ /\b[3 2 5 11]\b/' test_lines.txt
>> Line 1
>> Line 2
>> Line 3
>> Line 5
>>
>> ~$ perl -ne 'print if $. =~ /\b[3,2, 5, 11]\b/' test_lines.txt
>> Line 1
>> Line 2
>> Line 3
>> Line 5
>>
>> On Mon, Aug 31, 2020 at 8:17 AM Brian Duggan  wrote:
>> >
>> > On Monday, August 31, Andy Bach wrote:
>> > > >  raku -ne '.say if $++ == 3|2|5' Lines.txt
>> > >
>> > > OT, maybe, but is
>> > > perl -ne 'print if $. =~ /\b[325]\b/' Lines.txt
>> > >
>> > > or
>> > > perl -ne 'print if $c++ =~ /\b[436]\b/' Lines.txt
>> > >
>> > > the best you can do in P5?
>> >
>> > I can't think of anything better :-)
>> >
>> > Brian


Re: print particular lines question

2020-08-31 Thread yary
Aww don't you remember Raku's earliest(?) contribution to Perl? I was so
happy when this arrived, and sad over its subsequent neglect

perl -ne 'no warnings "experimental"; print if $. ~~ [3,5,11]' line0-10.txt


-y


On Mon, Aug 31, 2020 at 8:28 AM William Michels via perl6-users <
perl6-us...@perl.org> wrote:

> How would P5 handle line numbers > 10 ? Not getting back line #11 with
> the P5 examples below:
>
> $ raku -ne '.say if ++$ == 3|2|5|11' test_lines.txt
> Line 2
> Line 3
> Line 5
> Line 11
>
> ~$ perl -ne 'print if $. =~ /\b[3 2 5 11]\b/' test_lines.txt
> Line 1
> Line 2
> Line 3
> Line 5
>
> ~$ perl -ne 'print if $. =~ /\b[3,2, 5, 11]\b/' test_lines.txt
> Line 1
> Line 2
> Line 3
> Line 5
>
> On Mon, Aug 31, 2020 at 8:17 AM Brian Duggan  wrote:
> >
> > On Monday, August 31, Andy Bach wrote:
> > > >  raku -ne '.say if $++ == 3|2|5' Lines.txt
> > >
> > > OT, maybe, but is
> > > perl -ne 'print if $. =~ /\b[325]\b/' Lines.txt
> > >
> > > or
> > > perl -ne 'print if $c++ =~ /\b[436]\b/' Lines.txt
> > >
> > > the best you can do in P5?
> >
> > I can't think of anything better :-)
> >
> > Brian
>


Re: print particular lines question

2020-08-31 Thread William Michels via perl6-users
How would P5 handle line numbers > 10 ? Not getting back line #11 with
the P5 examples below:

$ raku -ne '.say if ++$ == 3|2|5|11' test_lines.txt
Line 2
Line 3
Line 5
Line 11

~$ perl -ne 'print if $. =~ /\b[3 2 5 11]\b/' test_lines.txt
Line 1
Line 2
Line 3
Line 5

~$ perl -ne 'print if $. =~ /\b[3,2, 5, 11]\b/' test_lines.txt
Line 1
Line 2
Line 3
Line 5

On Mon, Aug 31, 2020 at 8:17 AM Brian Duggan  wrote:
>
> On Monday, August 31, Andy Bach wrote:
> > >  raku -ne '.say if $++ == 3|2|5' Lines.txt
> >
> > OT, maybe, but is
> > perl -ne 'print if $. =~ /\b[325]\b/' Lines.txt
> >
> > or
> > perl -ne 'print if $c++ =~ /\b[436]\b/' Lines.txt
> >
> > the best you can do in P5?
>
> I can't think of anything better :-)
>
> Brian


Re: print particular lines question

2020-08-31 Thread Brian Duggan
On Monday, August 31, Andy Bach wrote: 
> >  raku -ne '.say if $++ == 3|2|5' Lines.txt
> 
> OT, maybe, but is
> perl -ne 'print if $. =~ /\b[325]\b/' Lines.txt
> 
> or
> perl -ne 'print if $c++ =~ /\b[436]\b/' Lines.txt
> 
> the best you can do in P5?

I can't think of anything better :-)

Brian


Re: print particular lines question

2020-08-31 Thread Andy Bach
>  raku -ne '.say if $++ == 3|2|5' Lines.txt

OT, maybe, but is
perl -ne 'print if $. =~ /\b[325]\b/' Lines.txt

or
perl -ne 'print if $c++ =~ /\b[436]\b/' Lines.txt

the best you can do in P5?

a

Andy Bach, BS, MSCMECFA
Systems Mangler
Internet: andy_b...@wiwb.uscourts.gov
Voice: (608) 261-5738, Cell: (608) 658-1890

"The three great problems of computer science:
compiler complexity and 'off-by-one' errors".
https://martinfowler.com/bliki/TwoHardThings.html


From: Brian Duggan 
Sent: Monday, August 31, 2020 7:53 AM
To: Curt Tilmes 
Cc: perl6-users 
Subject: Re: print particular lines question

On Monday, August 24, Curt Tilmes wrote:
> $ cat Lines.txt | raku -e '.say for lines()[3,2,5]'

The -n flag is an option here too:

   raku -ne '.say if $++ == 3|2|5' Lines.txt

Brian


Re: lines :$nl-in question

2020-08-31 Thread Stuckwisch, Matthew
So I guess I got included on this as the resident language professor :-) 
(although I probably should subscribe to p6 users at some point)  I didn't see 
the whole thread in the e-mail I got copied in on, so apologizes if I repeat 
much.

I'll spare everyone all the linguistic details, but suffice it to say, invocant 
and invoker are functionally equivalent.  The former is a nominalized 
adjective, and the latter a noun proper.  They mean "the one that invokes 
(calls)".   We can use either, but I'd recommend we be very consistent.  
Invocant seems to be the preferred, so let's use it exclusively.  On the other 
side, we have invoked and invokee, which in English share a similar distinction 
(although nominalizing the former often sounds odd, and adjectifying the latter 
would probably be read as missing a genitive 's).

Summed up, we should avoid using invocant to refer to a method that's being 
called.  Generally, just calling it a "method" is sufficient, but if need be, 
I'd go with "the invoked method".  We probably can write documentation to avoid 
the formalisms altogether, though, by saying something to the effect of

> Opens the file [represented by the [calling] object] and returns its lines.

That's fairly simple and clear.  In an article about signatures, however, I 
think using the term is absolutely appropriate, and warrants defining it 
parenthetically inline as "the object that calls/invokes the associated 
method".  I get that using the word "call" muddies the water (the formal 
distinction that tchrist was getting at is that a sub has no invocant except 
perhaps a calling context, ie the "caller" — which a method also has), but we 
already did that by having methods use CALL-ME instead of INVOKE-ME :-) *

MSS


* Of course, the reason was to harmonize calls of all code, and the fluid 
nature of allowing transitive methods be used as subs and viceversa (method 
$invocant: @args and $invocant.) means that one term had to rule them all.


From: William Michels mailto:w...@caa.columbia.edu>>
Sent: Sunday, August 30, 2020 2:44:55 PM
To: yary mailto:not@gmail.com>>
Cc: perl6-users mailto:perl6-us...@perl.org>>; 
ToddAndMargo mailto:toddandma...@zoho.com>>; Brad 
Gilbert mailto:b2gi...@gmail.com>>
Subject: Re: lines :$nl-in question

Do you agree with that definition, Yary? Brad? Here it is:

"Invocant"

"Caller, the one who calls or invokes. The invocant of a method would
be the object on which that method is being called, or, in some cases,
the class itself. Invocant is used instead of caller because the
latter refers to the scope."

https://docs.raku.org/language/glossary#Invocant

At first blush, the definition at
https://docs.raku.org/language/glossary#Invocant contradicts the
definition given to us by Brad. English speaker will typically use the
following word pairs to denote 1. an actor and 2. a recipient of some
action. So we have the following:

Payer vs. Payee
Lessor vs. Lessee
Employer vs. Employee

So going with the typical English usage above, the pattern would
continue with "Caller" vs "Callee" and "Invoker" vs
"Invokee/Invocant".  Therefore my humble reading of the definition
given by Brad, as well as a post authored by a certain TChrist on
StackExchange [1], suggests to me that "Invocant" is a synonym for
"Callee" (or the possibly-imaginary word "Invokee"). One can look at
the definition of "Invoker" online provided by Oracle with regards to
the Java programming language [2], to further distinguish "Invoker" vs
"Invocant".

HTH, Bill.

W. Michels, Ph.D.

[1] https://english.stackexchange.com/a/59070
[2] 
https://docs.oracle.com/javase/8/docs/api/index.html?javax/xml/ws/spi/Invoker.html


On Sun, Aug 30, 2020 at 9:54 AM yary 
mailto:not@gmail.com>> wrote:
>
> The Raku glossary has a definition
> https://docs.raku.org/language/glossary#Invocant
>
> suggestion, link to that where the term appears.
>
> -y
>
>
> On Sun, Aug 30, 2020 at 9:16 AM William Michels via perl6-users 
> mailto:perl6-us...@perl.org>> wrote:
>>
>> Inline:
>>
>> On Sun, Aug 30, 2020 at 12:49 AM Brad Gilbert 
>> mailto:b2gi...@gmail.com>> wrote:
>> >
>> > Invocant is in the dictionary though.
>> >
>> > In fact it is from Latin.
>> >
>> > Origin & history:
>> >   Derived from in- + vocō ("I call").
>> >
>> > Verb:
>> >   I invoke
>> >   I call (by name)
>> >
>> > In fact that is pretty close to the same meaning as it is used in the Raku 
>> > docs.
>> >
>> > It is the object that we are calling (aka invoking) a method on.
>>
>> Maybe we can meet Todd halfway?
>>
>> >
>> > On Sat, Aug 29, 2020 at 6:39 PM ToddAndMargo via perl6-users 
>> > mailto:perl6-us...@perl.org>> wrote:
>> >>
>> >> On 2020-08-28 23:51, Tobias Boege wrote:
>> >> > On Fri, 28 Aug 2020, ToddAndMargo via perl6-users wrote:
>> >> >> https://docs.raku.org/type/IO::Path#method_lines
>> >> >>
>> >> >> (IO::Path) method lines
>> >> >>
>> >> >> Defined as:
>> >> >>
>> >> >> method lines(IO::Path:D: 

Re: print particular lines question

2020-08-31 Thread Brian Duggan
On Monday, August 24, Curt Tilmes wrote: 
> $ cat Lines.txt | raku -e '.say for lines()[3,2,5]'

The -n flag is an option here too:

   raku -ne '.say if $++ == 3|2|5' Lines.txt

Brian


pod6 and markdown

2020-08-31 Thread Fernando Santagata
Hello *,

I was wondering whether there's a way to tell that a section of pod6 should
be rendered only by a specific renderer.

My problem is that I want to show a figure in a README.md and I'm using
App::MI6, which builds the README.md file from the pod6 documentation in
the module file.

As far as I can tell, there's no specific pod6 formatter for a figure; so
far I 've beeninserting raw markdown lines in the pod6 documentation, such
as "![description](file.svg)", but it's ugly and it would show when other
renderers will be used on the same pod6 file.

Is there a way to restrict some text to just one renderer, or to insert a
figure or picture in a pod6 file?

-- 
Fernando Santagata