At Sat, 14 Jul 2018 04:42:35 -0700 (PDT) Alexandru <
alexandr...@meshparts.de> wrote:
>
> Am Samstag, 14. Juli 2018 04:29:02 UTC+2 schrieb briang:
> > > Yes... I get it now... No exception, just the normal rules.
> > > I'm just saying, that it's very easy for me to program errors like this=
> . I don't know how other people see this.=20
> > > Besides, I was wondering if it would be an improvement for "expr" to al=
> so perform substitutions on the operators.
> >=20
> > This is a very interesting statement and question. Having spent >20 year=
> s programming in Tcl, writing 100's of thousands of lines of code, and havi=
> ng to maintain it, I have long ago learned that it never pays to get tricky=
> or cute with coding. Writing stuff like [expr {3 $op 4}] is not useful, i=
> t's just a bug waiting to happen. So no, it would be to the detriment of T=
> cl to allow substations like this.
> >=20
> > Code should always be clear and simple. If it's not, a future you will n=
> ot be able to understand what the present you wrote, and will curse you six=
> ways to Sunday for writing such a mess. (been there, done that.)
> >=20
> > -Brian
>
> I know what you mean. I also like to code things clearly and without compli=
> cations. Here is the procedure, where I use $op. What do think? How would y=
> ou write it?
>
> ## Get first element in list matching the expression giving by the operator=
> "op" with respect to the referrence value "ref".
> # \param l list of values or sublists of values
> # \param op operator as in Tcl (e.g. >, <, =3D=3D etc.)
> # \param ref referrence value to compare list elements to
> # \param index index for sublists
> # \return list index of first matching list element
> proc ListFirst {l op ref {index ""}} {
> set idx 0
> foreach e $l {
> if {$index!=3D""} {
This is something of a variation of lsort, where there -increasing (>),
-decreasing (<), and -command ("etc.").
Rather than limiting it to a simple scalar compare, I would follow lsort's
lead and just allow a random predicate:
proc ListFirst {l predicate ref} {
set idx 0
foreach e $l {
set flag [uplevel #0 [list $predicate $e $ref]]
if {$flag} {
return $idx
}
incr idx
}
return -1
}
This allows predicate to be anything and allows the elements of l to be
anything (including sub-lists (of *arbitary* depth), or other things, like
keys into an array, SNIT objects, canvas graphic element indexes, etc.) and
the predicate can do whatever test you need, beyond the *limited* comparison
ops available in expr. The predicate can handle whatever structure you need to
handle. And you don't need index, since you would embed its functionallity
into the predicate. This *simplfies* the function and makes it more general
purpose.
>
--
Robert Heller --
978-544-6933
Deepwoods Software -- Custom Software Services
http://www.deepsoft.com/ -- Linux Administration Services
hel...@deepsoft.com -- Webhosting Services