Re: BEGIN {} question

2022-09-03 Thread ToddAndMargo via perl6-users

On 9/2/22 18:14, ToddAndMargo via perl6-users wrote:

On 9/2/22 13:52, ToddAndMargo via perl6-users wrote:

On 9/2/22 00:13, ToddAndMargo via perl6-users wrote:

Found something interesting

$ raku -c GetUpdates.pl6
Syntax OK

Will execute the BEGIN {}, not just
syntax check it.


The guys on the chat line said this is normal
as `BEGIN` runs a compile time




Hi All,

Thinking about it, I thought I did not bring
enough attention to the `-c` switch in the
above command line.  This runs a SYNTAX check
and stops at that.  It does not run the program.
I use the `-c` option extensively to debug my
typos before debugging my programs.

When I found BEGIN actually running when all
I wanted was a syntax check, I was perplexed.
My understanding was that -c only checked my
syntax, including my BEGIN block.

This is why I asked on the chat line. Bug
or suppose to be?  And the answer is that
is just turned out that way.  And that is
fine with me.

:-)

-T


Hi All,

For the fun of it, I placed a "booboo;"
in the BEGIN block to see what the syntax
checker would do:

$ raku -c GetUpdates.pl6
===SORRY!=== Error while compiling /home/linuxutil/GetUpdates.pl6
Undeclared routine:
booboo used at line 28

Caught it.  No BEGIN pop up



Then I moved the booboo to the end of the
program

$ raku -c GetUpdates.pl6
===SORRY!=== Error while compiling /home/linuxutil/GetUpdates.pl6
Undeclared routine:
booboo used at line 11664

Caught it.  And I also got the BEGIN's pop up.

Interesting!

:-)

-T

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


Re: steps of a path

2022-09-03 Thread Ralph Mellor
Marc Chantreux  wrote:
>
> I got ([^1,^2,^3]) and tried to generalize it with something
> more generic (using * to get the number of elements).

Yeah, I was disappointed that that didn't work, and that what
did was relatively ugly, which is why I didn't bother to share it.

<<. raku -ne '.Str.say for m:ex{^ [:r "/" <-[/]>+]+? }'

The `.Str.say` can be just `.put`.

I'm surprised about your preference.

Is that because you're unfamiliar with Bruce's suggestion
(cumulative concat), or worry others will be unfamiliar with it?

If not, can you see why I'm surprised -- why `m:g{ "/" <-[/]>+ }`
seems simpler to me than `m:ex{^ [:r "/" <-[/]>+]+? }`?

--
raiph


Re: steps of a path

2022-09-03 Thread William Michels via perl6-users
Hi Marc, There's also this conversation from March 2021 on the mailing list:

https://www.nntp.perl.org/group/perl.perl6.users/2021/03/msg9857.html

[Matthew's answer looks very interesting].

Anyway, HTH. --Bill.


On Sat, Sep 3, 2022 at 2:51 PM Marc Chantreux  wrote:

> On Sat, Sep 03, 2022 at 09:50:08PM +0100, Ralph Mellor wrote:
> > > ( A B C ) to ((A) (A B) (A B C)) ?
> > [^1,^2,^3]
>
> I got that one and tried to generalize it with something more generic
> (using * to get the number of elements).
>
> thanks for helping
>
> --
> Marc Chantreux
> Pôle de Calcul et Services Avancés à la Recherche (CESAR)
> http://annuaire.unistra.fr/p/20200
>


Re: steps of a path

2022-09-03 Thread Marc Chantreux
On Sat, Sep 03, 2022 at 09:50:08PM +0100, Ralph Mellor wrote:
> > ( A B C ) to ((A) (A B) (A B C)) ?
> [^1,^2,^3]

I got that one and tried to generalize it with something more generic
(using * to get the number of elements).

thanks for helping

-- 
Marc Chantreux
Pôle de Calcul et Services Avancés à la Recherche (CESAR)
http://annuaire.unistra.fr/p/20200


Re: steps of a path

2022-09-03 Thread Marc Chantreux
Hi Bruce and William,

Ineed: I was looking for [\,] but your code removes the anoying empty
string because of the leading / (which is awesome) so I mixed from both
answers (<-[/]> is more robust than .alpha ) and added .Str to .say.

finally I got:

<<. raku -ne '.Str.say for m:ex /^ ["/" <-[/]>+:] **? 1..*  /;'
/var/log/messages

ther is only one caracter I don't understand in this anwser so I
considered removing it. bad idea:

<<. raku -ne '.Str.say for m:ex /^ ["/" <-[/]>+ ] **? 1..*  /;'
/var/log/messages

so I read about backtracking and found something I found a bit easier
to get/memorize: the :r adverb.

<<. raku -ne '.Str.say for m:ex{ ^ [ :r "/" <-[/]>+ ] **? 1..* }'
/var/log/messages

last but not least: **? 1..* is just +?.  At this point, my prefered solution 
is:

<<. raku -ne '.Str.say for m:ex{^ [:r "/" <-[/]>+]+? }'
/var/log/messages

and it is pretty good compared to the sed version:

<<. sed -E ':b p; s:/[^/]+$::; t b'

thank you very much to both of you: I learned a lot on this post.

-- 
Marc Chantreux
Pôle de Calcul et Services Avancés à la Recherche (CESAR)
http://annuaire.unistra.fr/p/20200


Re: steps of a path

2022-09-03 Thread Ralph Mellor
On Sat, Sep 3, 2022 at 9:50 PM Ralph Mellor  wrote:
>
> .put for [\~] '/A/B/C' ~~ m:g { '/'? <-[/]>+ }

That won't match just a slash (`/`). Maybe:

.put for [\~] '/A/B/C' ~~ m:g { ('/'? <-[/]>*)  }

And it'll treat `/a/b` and `/a/b/` as distinct if the input string is `/a/b/`.

--
raiph


Re: steps of a path

2022-09-03 Thread Ralph Mellor
On Sat, Sep 3, 2022 at 9:50 PM Ralph Mellor  wrote:
>
> it makes more sense to do something like Bruce or Michel's solutions.

s/Michel/William/

--
raiph


Re: steps of a path

2022-09-03 Thread Ralph Mellor
On Sat, Sep 3, 2022 at 6:17 PM Marc Chantreux  wrote:
>
> ( A B C ) to ((A) (A B) (A B C)) ?

[^1,^2,^3]

I could share a generalization but it's pretty ugly and I also think
it makes more sense to do something like Bruce or Michel's solutions.
Here's my variation:

.put for [\~] '/A/B/C' ~~ m:g { '/'? <-[/]>+ }

--
raiph


Re: steps of a path

2022-09-03 Thread William Michels via perl6-users
Hi Marc (and Bruce)!

Okay, I use our old friend `:exhaustive` adverb below:

~$ echo "/var/log/messages" | raku -ne '.say for m:ex/ ^ ["/"
<.alpha>+:]**?{1..*}  /;'
「/var」
「/var/log」
「/var/log/messages」

If you remove the `?` frugal quant-modifier, the output is the same--except
in the reverse order.

HTH, Bill.

On Sat, Sep 3, 2022 at 12:45 PM Bruce Gray  wrote:

>
> > On Sep 3, 2022, at 12:17 PM, Marc Chantreux  wrote:
>
> --snip--
>
> > I thought the raku one could be shorter
>
> It will be hard to beat the brevity of a language with single-character
> instructions.
>
> --snip--
>
> > I'm pretty sure I saw a very concise and elegant way to transform
> > ( A B C ) to ((A) (A B) (A B C))
>
> Perhaps you are remembering `produce()`, also called "triangular reduce":
> https://docs.raku.org/routine/produce
> https://docs.raku.org/language/operators#Reduction_metaoperators
>
> $ raku -e 'say [\,] ;'
> ((A) (A B) (A B C))
>
> $ echo /var/log/messages | raku -ne '.say for [\~] .comb: /\/<-[/]>+/;'
> /var
> /var/log
> /var/log/messages
>
> --
> Hope this helps,
> Bruce Gray (Util of PerlMonks)
>
>


Re: steps of a path

2022-09-03 Thread Bruce Gray


> On Sep 3, 2022, at 12:17 PM, Marc Chantreux  wrote:

--snip--

> I thought the raku one could be shorter

It will be hard to beat the brevity of a language with single-character 
instructions.

--snip--

> I'm pretty sure I saw a very concise and elegant way to transform
> ( A B C ) to ((A) (A B) (A B C))

Perhaps you are remembering `produce()`, also called "triangular reduce":
https://docs.raku.org/routine/produce
https://docs.raku.org/language/operators#Reduction_metaoperators

$ raku -e 'say [\,] ;'
((A) (A B) (A B C))

$ echo /var/log/messages | raku -ne '.say for [\~] .comb: /\/<-[/]>+/;' 
/var
/var/log
/var/log/messages

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



steps of a path

2022-09-03 Thread Marc Chantreux
hello people,

I have this steps() function implemented this way in shell:

steps() sed -r ':b p; s,/[^/]+$,,; t b'
# demo:
<<. steps | xargs ls -lUd
/var/log/messages

which shows

-rw-r-  1 root adm  464304 Sep  3 19:03 /var/log/messages
drwxr-xr-x 22 root root   4096 Sep  3 00:00 /var/log
drwxr-xr-x 14 root root   4096 Jun 19  2021 /var

for those who don't know sed that much, the perl equivalent is:

steps() perl -lpe 'do {print} while s,/[^/]+$,,'

I thought the raku one could be shorter but for now I'm stuck
with a very long solution.

my \path = [ "/var/log/messages" .split: "/" ];
.say for (^path).map( { path[0..$_].join: "/" } )[1..*];

I'm pretty sure I saw a very concise and elegant way to transform
( A B C ) to ((A) (A B) (A B C)) in the past but I'm enable to figure
out how. Any help on that ?

thanks by advance.

-- 
Marc Chantreux
Pôle de Calcul et Services Avancés à la Recherche (CESAR)
http://annuaire.unistra.fr/p/20200