Sunday, March 28, 2010

Re: user function for multiple substitution

Tony Mechelynck wrote:
>> s/\c[scujgh]x/\=get({
>> 'sx':'ŝ',
>> 'cx':'ĉ',
>> 'ux':'ŭ',
>> 'jx':'ĵ',
>> 'gx':'ĝ',
>> 'hx':'ĥ',
>> 'SX':'Ŝ',
>> 'CX':'Ĉ',
>> 'UX':'Ŭ',
>> 'JX':'Ĵ',
>> 'GX':'Ĝ',
>> 'HX':'Ĥ'
>> }, submatch(0), '??default??')/g
>>
>> (which should have the benefit of a linear lookup time, and is a lot
>> less hassle to maintain, IMHO)
>
> For the default ("not found in table"), I'd just use submatch(0) again,
> i.e., "don't change".

Oh, duh...that's so obvious in retrospect. Glad I've got you to
keep me in line :-)

> Oh, and for logarithmic time my solution could use dichotomic searching
> (taking advantage of the fact that both the result_if_true and the
> result_if_false of a ?: construct can in turn be ?: expressions, each of
> which can, etc.) but for such a small set of possibilities I don't think
> there would be a very big performance gain.

I'm not sure how vim compiles expressions, and if, in my
suggestion, the in-line dict is created at every substitution, or
it's just parsed once and then reused internally -- that could
have a grave impact on my suggested solution. However it could
be mitigated by setting it up as an external constant (being
evaluated/compiled just once) dict and then just referenced in
the :s command if performance is a big deal. Though I agree with
your base premise that "for such a small set of possibilities",
we're arguing the number of angels on a pinhead. "this 3 second
operation took me 3.0001ms with Tim's solution and 3.0002ms with
Tony's solution"...time we've clearly thrown out the window
merely by discussing it :-)

-tim

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe from this group, send email to vim_use+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.

No comments:

Post a Comment