Re: steps of a path

2022-09-07 Thread William Michels via perl6-users
Apologies: the first code example in my previous email won't check for a
given directory, and will print out bogus paths:

~$ echo "/Users/none/bogus_dir/" | raku -e 'for lines.IO -> $a is copy
{$a.Str.say; repeat {$a.=parent andthen $a.Str.say;} until $a eq
$*SPEC.rootdir};'
/Users/none/bogus_dir/
/Users/none
/Users
/

The second code example will first check for a given directory:

~$ echo "/Users/admin/Documents/" | raku -e 'for lines.IO -> $a is copy {if
$a.d {$a.Str.say; repeat {$a.=parent andthen $a.Str.say;} until $a eq
$*SPEC.rootdir}};'
/Users/admin/Documents/
/Users/admin
/Users
/
~$ echo "/Users/none/bogus_dir/" | raku -e 'for lines.IO -> $a is copy {if
$a.d {$a.Str.say; repeat {$a.=parent andthen $a.Str.say;} until $a eq
$*SPEC.rootdir}};'
~$

https://docs.raku.org/type/IO::Path#File_test_operators

HTH, Bill.

On Wed, Sep 7, 2022 at 7:39 AM William Michels 
wrote:

> Hi Marc,
>
> Does this do what you want? I've omitted the call to `run` and used mostly
> IO::Path calls instead:
>
> ~$ echo "/Users/admin/logs" | raku -e 'for lines.IO -> $a is copy
> {$a.Str.say; repeat {$a.=parent andthen $a.Str.say;} until $a eq
> $*SPEC.rootdir;};'
>
> #returns:
>
> /Users/admin/logs
> /Users/admin
> /Users
> /
>
> Note, above won't actually check if the given directory is valid--it only
> checks the parents. To check a given directory first, try the following:
>
> ~$ echo "/Users/admin/bogus_dir/" | raku -e 'for lines.IO -> $a is copy
> {if $a.d {$a.Str.say; repeat {$a.=parent andthen $a.Str.say;} until $a eq
> $*SPEC.rootdir}};'
>
> #returns nothing for `bogus_dir`
>
> HTH, Bill.
>
>
> On Mon, Sep 5, 2022 at 1:56 PM Marc Chantreux  wrote:
>
>> I love this one. I used uniq and run so the whole script can be run from
>> raku (except the xargs ls avoid the ARG_MAX error)
>>
>> <<. raku -e 'run < ls -lUd >, unique map {(.IO, *.parent …^
>> "/")>>.Str.Slip}, lines'
>> /var/log/messages
>> /var/log/auth.log
>>
>> regards
>>
>> --
>> 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-07 Thread The Sidhekin
On Wed, Sep 7, 2022 at 6:27 PM Parrot Raiser <1parr...@gmail.com> wrote:

> >
> > That said, right now gmail is claiming whipupitude is misspelled...
> >
> An alternative is "whipitupitude" (the difference being the first "it".
>
> Given the examples I've seen over the years, there's a need for an
> opposite to "idiomatic", for programming that arrives at a solution by
> a Rube Goldberg/Heath Robinson sort of convoluted and unnecessary
> path, or utterly abuses the language in a fashion that contradicts its
> underlying purpose or philosophy (writing video games in sed or
> invoicing programs in Lisp). "Idiotmatic" suggests itself. :-)*
>

  Sure you don't want "idiosyncratic"?


Eirik


Re: steps of a path

2022-09-07 Thread Parrot Raiser
>
> That said, right now gmail is claiming whipupitude is misspelled...
>
An alternative is "whipitupitude" (the difference being the first "it".

Given the examples I've seen over the years, there's a need for an
opposite to "idiomatic", for programming that arrives at a solution by
a Rube Goldberg/Heath Robinson sort of convoluted and unnecessary
path, or utterly abuses the language in a fashion that contradicts its
underlying purpose or philosophy (writing video games in sed or
invoicing programs in Lisp). "Idiotmatic" suggests itself. :-)*


Re: steps of a path

2022-09-07 Thread William Michels via perl6-users
Hi Marc,

Does this do what you want? I've omitted the call to `run` and used mostly
IO::Path calls instead:

~$ echo "/Users/admin/logs" | raku -e 'for lines.IO -> $a is copy
{$a.Str.say; repeat {$a.=parent andthen $a.Str.say;} until $a eq
$*SPEC.rootdir;};'

#returns:

/Users/admin/logs
/Users/admin
/Users
/

Note, above won't actually check if the given directory is valid--it only
checks the parents. To check a given directory first, try the following:

~$ echo "/Users/admin/bogus_dir/" | raku -e 'for lines.IO -> $a is copy {if
$a.d {$a.Str.say; repeat {$a.=parent andthen $a.Str.say;} until $a eq
$*SPEC.rootdir}};'

#returns nothing for `bogus_dir`

HTH, Bill.


On Mon, Sep 5, 2022 at 1:56 PM Marc Chantreux  wrote:

> I love this one. I used uniq and run so the whole script can be run from
> raku (except the xargs ls avoid the ARG_MAX error)
>
> <<. raku -e 'run < ls -lUd >, unique map {(.IO, *.parent …^
> "/")>>.Str.Slip}, lines'
> /var/log/messages
> /var/log/auth.log
>
> regards
>
> --
> 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-07 Thread Ralph Mellor
On Wed, Sep 7, 2022 at 1:20 AM Marc Chantreux  wrote:
>
> Actually what I really like the most from all your answers was the fact
> that I learned a lot not only about the langage but also some idoms.

That sounds cool. :)

I know what *I* think of when I write "idiom" in the context of programming
languages. But what do *you* mean? Or, even better, can you identify a
few of the many useful suggestions folk have made in this thread which
you would count as being, or possibly worthy of being, "idioms"?

Wikipedia notes two uses of the term "idiom" in this context (claiming the first
is correct but the second isn't, though the distinction seems immaterial here):

> code [with a] semantic role, which recurs frequently across software projects

> using a programming language in a typical way

I know I wasn't particularly aiming at either of those, but instead playing golf
in response to what I thought you were after, and focusing on:

* First and foremost, Larry's notion of "clean golf" (minimal tokens,
not bytes).

* Secondarily my notion of "semantic golf" (minimal mental effort when reading).

In other words my goal was code requiring minimal reading effort to understand
while still appropriately expressing the desired computational result
and/or effect.



> > Is that because it knows me, or has google started blessing Larry's
> > neologisms for the whole planet?!? )

I now think it (gmail and/or my brain; they may or may not have
merged) glitched on me.

> Why not? new words happens all the time and those one are useful for
> programmers.

I had done a google for "whipupitude" before I wrote the thought bubble in
my previous message. Google claimed it found 300 results. I just tried again;
adding -perl shrank their claim to just 8 matches.

That said, right now gmail is claiming whipupitude is misspelled...

--
raiph



>
> --
> 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-06 Thread Marc Chantreux
Hi Ralph,

> Given that the broader picture is file/path related operations, it makes
> sense to me you're mostly exploring use of `.parent` and `map`s etc.

Actually what I really like the most from all your answers was the fact
that I learned a lot not only about the langage but also some idoms.

> Is that because it knows me, or has google started blessing Larry's
> neologisms for the whole planet?!? )

Why not? new words happens all the time and those one are useful for
programmers.

-- 
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-06 Thread Ralph Mellor
On Mon, Sep 5, 2022 at 8:07 AM Marc Chantreux  wrote:
>
> I just played again with your solution and got the beauty of it.
>
> Actually: this is by far the simplest solution. Thanks.

Thanks for the follow up. So I'm not going mad! :)

Given that the broader picture is file/path related operations, it makes
sense to me you're mostly exploring use of `.parent` and `map`s etc.

Manipulexity is just as important as whipupitude!

.oO ( gmail's spell checker corrected my spelling of whipupitude, letting me
   know that it considered whipuptitude (with an extra 't') misspelled.
   Is that because it knows me, or has google started blessing Larry's
   neologisms for the whole planet?!? )

That said, a big part of my focus was due to:

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

and

> I'm pretty sure I saw a very concise and elegant way to... Any help on that ?

And when I see that kind of thing I tend to pop my "clean golf" mental hat on.

So it was good to hear the lead commentator appreciate my swing at it. :)

--
raiph


Re: steps of a path

2022-09-05 Thread Marc Chantreux
I love this one. I used uniq and run so the whole script can be run from
raku (except the xargs ls avoid the ARG_MAX error)

<<. raku -e 'run < ls -lUd >, unique map {(.IO, *.parent …^ "/")>>.Str.Slip}, 
lines'
/var/log/messages
/var/log/auth.log

regards

-- 
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-05 Thread Bruce Gray
> On Sep 5, 2022, at 1:54 AM, Marc Chantreux  wrote:
--snip of Bill's pointer to Mathew's email--
> I tried this line but got an immutability problem. I tried
> multiple work around with no success for the moment.
> <<. raku -e 'lines.IO.map: {repeat {.put} while not .=parent ~~ "/" }'
> /var/log/messages
--snip--

> On Sep 5, 2022, at 2:40 AM, Fernando Santagata  
> wrote:
> you can get around the immutability problem:
> raku -e '"/var/log/messages".IO.map: -> $_ is copy {repeat {.put} while ! ($_ 
> .= parent ~~ "/") }'

--snip--

The use of `.parent` inspired me to think of this shorter solution, via the 
Sequence Operator:
$ raku -e '.put for "/var/log/messages".IO, *.parent …^ "/";'
/var/log/messages
/var/log
/var

https://docs.raku.org/language/operators#infix_...

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



Re: steps of a path

2022-09-05 Thread Fernando Santagata
Hello,

you can get around the immutability problem:

raku -e '"/var/log/messages".IO.map: -> $_ is copy {repeat {.put} while !
($_ .= parent ~~ "/") }'


On Mon, Sep 5, 2022 at 8:55 AM Marc Chantreux  wrote:

> hello William,
>
> On Sat, Sep 03, 2022 at 04:27:04PM -0700, William Michels wrote:
> > 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].
>
> Interesting as it can provide a relative short solution with no advanced
> concept. I tried this line but got an immutability problem. I tried
> multiple work around with no success for the moment.
>
> <<. raku -e 'lines.IO.map: {repeat {.put} while not .=parent ~~ "/" }'
> /var/log/messages
>
> thank you
>
> --
> Marc Chantreux
> Pôle de Calcul et Services Avancés à la Recherche (CESAR)
> http://annuaire.unistra.fr/p/20200
>


-- 
Fernando Santagata


Re: steps of a path

2022-09-05 Thread Marc Chantreux
Hi Ralph,

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

oh! got it. thanks :)

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

I just played again with your solution and got the beauty of it. why
made me realize I should take time between two post so I can have a
fresh mindset for all of them!

Actually: this is by far the simplest solution. Thanks.


-- 
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-05 Thread Marc Chantreux
hello William,

On Sat, Sep 03, 2022 at 04:27:04PM -0700, William Michels wrote:
> 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].

Interesting as it can provide a relative short solution with no advanced
concept. I tried this line but got an immutability problem. I tried
multiple work around with no success for the moment.

<<. raku -e 'lines.IO.map: {repeat {.put} while not .=parent ~~ "/" }'
/var/log/messages

thank you

-- 
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
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