tcl8.5 new operator in expr 
Author Message
 tcl8.5 new operator in expr

While it is now really easy to add new (any-adic) functions
to expr, like:   (I love 8.5 :-)
  interp alias {} ::tcl::mathfunc::list {} list
  set xy [expr {list($r*cos($phi),$r*sin($phi))}]
or
  proc ::tcl::mathfunc::like {v p} {string match $p $v}
  if {like($val,"abc*")} ...

I wonder whether it will be possible someday (e.g. in final 8.5)
to also add named operators, that could then be used like "eq"
and "ne".   Kevin?



Sat, 05 Jul 2008 20:25:30 GMT  
 tcl8.5 new operator in expr
The problem with adding new operators (as opposed to new functions) is
that they require changes to the way that expressions are tokenized and
parsed. That's quite a lot harder to make work flexibly.

Donal.



Sat, 05 Jul 2008 20:44:52 GMT  
 tcl8.5 new operator in expr
I do have to agree here. The "ne" and "eq" operators have made "if"
comparisons considerably less verbose - To my mind the only (widely
used) things missing now are "glob" and "regexp" string comparisons.
Some thing like:-

if {$myvar like *glob*}

...and...

if {$myvar match {^[Rr]egexp$}}

I have no idea how complex these would be to implement, but I can't
imagine any Tcl/Tk programmer who wouldn't like to see something along
these lines!!

Best Regards,
   =Adrian=



Sun, 06 Jul 2008 17:01:05 GMT  
 tcl8.5 new operator in expr

Adrian Davis schrieb:

Quote:
> I have no idea how complex these would be to implement, but I can't
> imagine any Tcl/Tk programmer who wouldn't like to see something along
> these lines!!

I for one like "like" - the negated version might be "unlike" then,
just as eq/ne, in/ni ...


Sun, 06 Jul 2008 17:34:34 GMT  
 tcl8.5 new operator in expr

Quote:

> I do have to agree here. The "ne" and "eq" operators have made "if"
> comparisons considerably less verbose - To my mind the only (widely
> used) things missing now are "glob" and "regexp" string comparisons.
> Some thing like:-

> if {$myvar like *glob*}

> ...and...

> if {$myvar match {^[Rr]egexp$}}

> I have no idea how complex these would be to implement, but I can't
> imagine any Tcl/Tk programmer who wouldn't like to see something along
> these lines!!

> Best Regards,
>    =Adrian=

This has been in tcl for a long time, it is called [switch]:

switch -exact|-glob|-regexp

switch -regexp -- $myvar \
          {^[Rr]egexp$} {
        } default {
        }

It can be usefull to composite the argument like

switch -- $test1,$subtest2 \
         VALUE_A,A_SUB_VALUE_1 {
        } ....

which is IMHO very often the best solution to c-style if-spagetti
and clean looking too.

uwe



Sun, 06 Jul 2008 18:32:09 GMT  
 tcl8.5 new operator in expr
Wow - the first time I realize, that I can extend the expr command with
our own vector procedures!

Wonderful!

But ... I don't see much need for defining own operators.
Otherwise I wanted so often additional operators for glob and regexp
matching!

Best regards,

Martin Lemburg
UGS - Transforming the Process of Innovation



Sun, 06 Jul 2008 19:00:03 GMT  
 tcl8.5 new operator in expr
I'm aware of, and do use switch for this, but I still think that...

if {$myvar like *glob*}

...and...

if {$myvar match {^[Rr]egexp$}}

...are more intuative and easier to read than someting like...

 switch -regexp -- $myvar \
          {^[Rr]egexp$} {
        } default {
        }

Using "if" also allows one to mix in "and" and "or" conditions in a
natural way...

if {$myvar1 like *glob* && $myvar2 > 99}

...A personal preference I would agree - though I think many others
would agree with me.

Best Regards,
   =Adrian=



Sun, 06 Jul 2008 20:50:31 GMT  
 tcl8.5 new operator in expr
: I'm aware of, and do use switch for this, but I still think
that...
: if {$myvar like *glob*}
: ...and...
: if {$myvar match {^[Rr]egexp$}}

But what's wrong with

if {[string match *glob* $myvar]}
and

if {[regexp {^[Rr]egexp$} $myvar]}

?
--



Sun, 06 Jul 2008 21:20:15 GMT  
 tcl8.5 new operator in expr
Just seems to me to feel like a more natual "if" type syntax - That's
all.

Regards,
   =Adrian=



Sun, 06 Jul 2008 21:23:43 GMT  
 tcl8.5 new operator in expr

Quote:

> I'm aware of, and do use switch for this, but I still think that...

> if {$myvar like *glob*}

> ...and...

> if {$myvar match {^[Rr]egexp$}}

> ...are more intuative and easier to read than someting like...

>  switch -regexp -- $myvar \
>           {^[Rr]egexp$} {
>         } default {
>         }

> Using "if" also allows one to mix in "and" and "or" conditions in a
> natural way...

> if {$myvar1 like *glob* && $myvar2 > 99}

switch -glob -- $condA,$condB \
          0,1   { + !A && B
        } 1,1   [ # A && B
        } 1,* - *,1 { # A || B
        } ...

Quote:

> ...A personal preference I would agree - though I think many others
> would agree with me.

You are certainly right, and on both counts.
But with tcl being stringier than c i have a (still) growing fondness
for [switch].

And some samples provided in the last couple of days would have gained
significantly in readability by using switch in place of repeated
if {$string eq A } {

Quote:
} else if  {$string eq B } {
} ....

> Best Regards,
>    =Adrian=

G!
uwe


Sun, 06 Jul 2008 21:27:50 GMT  
 tcl8.5 new operator in expr

Quote:
> I for one like "like" - the negated version might be "unlike" then,
> just as eq/ne, in/ni ...

IMO, "ni" is a *terrible* way to say "not in" -- it's far too easy to
accidentally type one when you mean the other, and it's not very distinct
visually.  if/fi, case/esac et all make sense in shell programming because
[a] they're paired and [b] typo'ing them causes parse errors.

in/notin is a better choice.

Regards,
Adrian



Sun, 06 Jul 2008 18:22:44 GMT  
 tcl8.5 new operator in expr

Quote:
> I do have to agree here. The "ne" and "eq" operators have made "if"
> comparisons considerably less verbose - To my mind the only (widely
> used) things missing now are "glob" and "regexp" string comparisons.
> Some thing like:-

> if {$myvar like *glob*}

> ...and...

> if {$myvar match {^[Rr]egexp$}}
Yes! nice idea.

> I have no idea how complex these would be to implement, but I can't
> imagine any Tcl/Tk programmer who wouldn't like to see something along
> these lines!!

> Best Regards,
>    =Adrian=



Sun, 06 Jul 2008 23:41:57 GMT  
 tcl8.5 new operator in expr

Quote:

> Wow - the first time I realize, that I can extend the expr command with
> our own vector procedures!

> But ... I don't see much need for defining own operators.

Just when you're talking about vektor procedures ...
Wouldn't a custom  expr {$v1 dot $v2} just for your scripts
make better readable code?

Quote:
> Otherwise I wanted so often additional operators for glob and regexp
> matching!

Yes, that was also the main drive behind my posting...

With  "like"/"unlike"   and perhaps "match"  I'd be satisfied
for a while even without a user-extensible general solution.



Sun, 06 Jul 2008 23:58:45 GMT  
 tcl8.5 new operator in expr

Quote:

> I wonder whether it will be possible someday (e.g. in
> final 8.5) to also add named operators, that could then
> be used like "eq" and "ne".

Would you then be able to override the meaning of
existing operators?

While being able to redefine "keywords" is very
tclish, I think redefining operators is going too far.

Keith



Mon, 07 Jul 2008 00:04:33 GMT  
 tcl8.5 new operator in expr

Quote:

> I do have to agree here. The "ne" and "eq" operators have made "if"
> comparisons considerably less verbose - To my mind the only (widely
> used) things missing now are "glob" and "regexp" string comparisons.
> Some thing like:-

> if {$myvar like *glob*}

> ...and...

> if {$myvar match {^[Rr]egexp$}}

> I have no idea how complex these would be to implement, but I can't
> imagine any Tcl/Tk programmer who wouldn't like to see something along
> these lines!!

The adding of expr operators isn't that hard (but may lead to
more bytecodes).  I've wanted glob/re expr operators for a
while as well.  I had considered '=' based operators, like:

        $a =* $b  # glob
        $a =~ $b  # re

The =~ is obviously from Perl, but they have more elaborate
results and syntax related to it that I wanted to avoid.  For
negation, you could have !* and !~.

As to the words ... I don't like the choices.  I might expect
SQL syntax for like, and using glob would be only 4 chars too
(but sounds clumsy).  The 'match' doesn't immediately invoke
re to me - why not use 're'?  But what to negate with?

--
   Jeff Hobbs, The Tcl Guy
   http://www.ActiveState.com/, a division of Sophos



Mon, 07 Jul 2008 00:38:07 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. tcl8.0.4 puts misbehaves in context of expr

2. [expr int()} and tcl8.0

3. Tcl8.0 expr command bug?

4. BUG: tcl8.0a2 expr compilation

5. tcl8's expr changes: why I think it's bad

6. A thought on the Tcl8 expr semantics

7. Tcl8.0a (another expr problem)

8. Aliases for operators, creating new operators

9. expr eq/ne operator puzzle

10. TIP #133: Extending [expr] Operators

11. TIP #123: Adding an Exponentiation Operator to the [expr] Command

12. expr operator precedence

 

 
Powered by phpBB® Forum Software