Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #293: You must've hit the wrong any key.


comp / comp.lang.tcl / Re: Parsing namespace name strings

SubjectAuthor
* Parsing namespace name stringsAlan Grunwald
`* Re: Parsing namespace name stringsEmiliano
 `- Re: Parsing namespace name stringsAlan Grunwald

1
Subject: Parsing namespace name strings
From: Alan Grunwald
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Fri, 29 Nov 2024 21:03 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nospam.nurdglaw@gmail.com (Alan Grunwald)
Newsgroups: comp.lang.tcl
Subject: Parsing namespace name strings
Date: Fri, 29 Nov 2024 21:03:25 +0000
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <vida8m$186cp$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 29 Nov 2024 22:04:22 +0100 (CET)
Injection-Info: dont-email.me; posting-host="571940df4d643858c6882180094191ef";
logging-data="1317273"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18snX7est+b5oLXHs0OHbVUbyvCKK0Rpio="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:bPGfXZ4qGPhTqLHNCX/dWVClSdc=
Content-Language: en-US
View all headers

I find I regularly want to know things like "what is first part of a
namespace name", "the first two parts" etc. I generally want to get the
same answer whether the name is a::b::c or ::a::b::c.

I can (and do) do this by getting a list of parts via something like

split [string map {"::" ":"} $name] ":"

but this is clunky - is there something like [namespace split] that
would return a list of parts?

For example, I'd like namespace split a::b::c to return {a b c}, and
namespace split ::d::e::f::g to return {d e f g}.

As a followup, if I write a proc namespaceSplit that does what I want,
is there a user-level way to modify the [namespace] command so that a
can execute namespaceSplit via [namespace split]?

Many thanks

Subject: Re: Parsing namespace name strings
From: Emiliano
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Fri, 29 Nov 2024 23:16 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: emiliano@example.invalid (Emiliano)
Newsgroups: comp.lang.tcl
Subject: Re: Parsing namespace name strings
Date: Fri, 29 Nov 2024 20:16:01 -0300
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <20241129201601.dde7fad0c89a3239a0ae6b8a@example.invalid>
References: <vida8m$186cp$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 30 Nov 2024 00:16:03 +0100 (CET)
Injection-Info: dont-email.me; posting-host="9d1ba3a732f9551777d5f906e292bc55";
logging-data="1356397"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ElSyV+CH8JhVUr9PBnzy40rXBrwApc1k="
Cancel-Lock: sha1:39wFjnorP3uXE3q8T00Bd2JIBL4=
X-Newsreader: Sylpheed 3.5.1 (GTK+ 2.24.32; i686-pc-linux-gnu)
View all headers

On Fri, 29 Nov 2024 21:03:25 +0000
Alan Grunwald <nospam.nurdglaw@gmail.com> wrote:

> I find I regularly want to know things like "what is first part of a
> namespace name", "the first two parts" etc. I generally want to get the
> same answer whether the name is a::b::c or ::a::b::c.
>
> I can (and do) do this by getting a list of parts via something like
>
> split [string map {"::" ":"} $name] ":"
>
> but this is clunky - is there something like [namespace split] that
> would return a list of parts?
>
> For example, I'd like namespace split a::b::c to return {a b c}, and
> namespace split ::d::e::f::g to return {d e f g}.

Easy combining [namespace tail] and [namespace qualifiers]:

proc ns2list {ns} {
set l {}
while {$ns ne {}} {
lappend l [namespace tail $ns]
set ns [namespace qualifiers $ns]
}
lreverse $l
}

% ns2list foo::bar::
foo bar {}
% ns2list foo::bar
foo bar
% ns2list ::::foo::bar::
foo bar {}
% ns2list ::foo::bar
foo bar
% ns2list {}
%

>
> As a followup, if I write a proc namespaceSplit that does what I want,
> is there a user-level way to modify the [namespace] command so that a
> can execute namespaceSplit via [namespace split]?

Since the [namespace] command is an ensemble, its easy to add a subcommand

% namespace ensemble configure namespace -map [dict merge [namespace ensemble configure namespace -map] {split ::ns2list}]
% namespace split ::foo::bar::baz
foo bar baz

>
> Many thanks

Regards
--
Emiliano

Subject: Re: Parsing namespace name strings
From: Alan Grunwald
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Sat, 30 Nov 2024 13:12 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nospam.nurdglaw@gmail.com (Alan Grunwald)
Newsgroups: comp.lang.tcl
Subject: Re: Parsing namespace name strings
Date: Sat, 30 Nov 2024 13:12:34 +0000
Organization: A noiseless patient Spider
Lines: 71
Message-ID: <vif31r$1nu0c$1@dont-email.me>
References: <vida8m$186cp$1@dont-email.me>
<20241129201601.dde7fad0c89a3239a0ae6b8a@example.invalid>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 30 Nov 2024 14:13:32 +0100 (CET)
Injection-Info: dont-email.me; posting-host="6af356727b6a4f4743bf56fcc8c3e6e8";
logging-data="1832972"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/c+mpMM+2Du/O+REf3k0KVGujoxXRl7UA="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:brHvty164f+b7F7usKYm6a8A7QA=
In-Reply-To: <20241129201601.dde7fad0c89a3239a0ae6b8a@example.invalid>
Content-Language: en-US
View all headers

On 29/11/2024 23:16, Emiliano wrote:
> On Fri, 29 Nov 2024 21:03:25 +0000
> Alan Grunwald <nospam.nurdglaw@gmail.com> wrote:
>
>> I find I regularly want to know things like "what is first part of a
>> namespace name", "the first two parts" etc. I generally want to get the
>> same answer whether the name is a::b::c or ::a::b::c.
>>
>> I can (and do) do this by getting a list of parts via something like
>>
>> split [string map {"::" ":"} $name] ":"
>>
>> but this is clunky - is there something like [namespace split] that
>> would return a list of parts?
>>
>> For example, I'd like namespace split a::b::c to return {a b c}, and
>> namespace split ::d::e::f::g to return {d e f g}.
>
> Easy combining [namespace tail] and [namespace qualifiers]:
>
> proc ns2list {ns} {
> set l {}
> while {$ns ne {}} {
> lappend l [namespace tail $ns]
> set ns [namespace qualifiers $ns]
> }
> lreverse $l
> }
>
> % ns2list foo::bar::
> foo bar {}
> % ns2list foo::bar
> foo bar
> % ns2list ::::foo::bar::
> foo bar {}
> % ns2list ::foo::bar
> foo bar
> % ns2list {}
> %
>
>>
>> As a followup, if I write a proc namespaceSplit that does what I want,
>> is there a user-level way to modify the [namespace] command so that a
>> can execute namespaceSplit via [namespace split]?
>
> Since the [namespace] command is an ensemble, its easy to add a subcommand
>
> % namespace ensemble configure namespace -map [dict merge [namespace ensemble configure namespace -map] {split ::ns2list}]
> % namespace split ::foo::bar::baz
> foo bar baz
>
>>
>> Many thanks
>
> Regards
Thanks Emiliano.

I did some testing after I posted last night, and I found that
manipulating the name with [string] commands ran faster than with
[namespace tail] and [namespace qualifiers]. I must admit that when
using the [namespace] subcommands, I was processing the list with
[linsert l 0 ...] rather than using [lappend l] and [lreverse] which
probably makes a difference.

Thanks also for the pointer about adding a new subcommand to the
namespace ensemble. I've never played with [namespace ensemble] before
and I can see why I was a little intimidated.

--

Alan

1

rocksolid light 0.9.8
clearnet tor