TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: cadman6735 on August 30, 2011, 12:35:35 PM

Title: (and (or *acad*
Post by: cadman6735 on August 30, 2011, 12:35:35 PM
I have been noticing code like this:

Code: [Select]
(and
  (or *acad* (setq *acad* (vlax-get-acad-object)))
  (or *doc* (setq *doc* (vla-get-activedocument *acad*)))

How does the (and (or statement benefit?

Thanks
Title: Re: (and (or *acad*
Post by: CAB on August 30, 2011, 12:41:57 PM
No benefit, just a creative use of the language.
IMO   8-)
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 12:53:19 PM
`creative use' is a stretch at best IMO. If that code were to be converted to plain English it would be "something" resembling utter gibberish.

Use COND in lieu of (and (or

I use:
Code: [Select]
(setq *acadobject*
         (cond
           (*acadobject*)
           ((vlax-get-acad-object))))

Far more clean and concise code wise.

The extension of that concept would be:
Code: [Select]
(setq *acadobject*
         (cond
           (*acadobject*)
           ((vlax-get-acad-object)))
      *activedocument*
         (cond
           (*activedocument*)
           ((vla-get-activedocument *acadobject*)))
      *documents*
         (cond
           (*documents*)
           ((vla-get-documents *acadobject*)))
...
Title: Re: (and (or *acad*
Post by: CAB on August 30, 2011, 01:02:28 PM
Beauty is in the eye of the beholder.

Sorry you only see ugliness.  :-(
Title: Re: (and (or *acad*
Post by: alanjt on August 30, 2011, 01:05:28 PM
I like the or method, but I also use a slight mod of Se7en's:

Code: [Select]
(cond (*acadobject*)
      ((setq *acadobject* (vlax-get-acad-object)))
)
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 01:15:34 PM
CAB, I don't see it as ugly. I see it as obfuscation for unknown reasons and code obfuscation is not something that should be in anyone's eye (beholders or users) IMO.

I'm sure you have heard the saying that programming languages are based upon the English language and to program you just assemble words into sentences and sentences into paragraphs; try and convert the above into a sentence then convert mine.

I posted a write up at one time on this subject but I'm not sure what it was called. I will/can/would be happy to look for it if you want to discuss (I really enjoy discussions like this quite a bit! I think they are a lot of fun). Let me know.
Title: Re: (and (or *acad*
Post by: Lee Mac on August 30, 2011, 01:15:59 PM
Another might be:

Code: [Select]
(if (null *acad*)
    (setq *acad* (vlax-get-acad-object))
)

I can see Se7en's viewpoint with regard to how the code 'reads' since the logical functions are more suited to cases where a boolean return is specifically required, and I would also avoid using the 'and' as in the first post, but I do agree that the use of 'or' for this purpose is quite clean and concise.
Title: Re: (and (or *acad*
Post by: Mark on August 30, 2011, 01:18:52 PM
English language my butt! Someone needs to put that in a sentence for me 'cause I don't understand.

I see no English in this. :)
Code: [Select]
(setq *acadobject*
         (cond
           (*acadobject*)
           ((vlax-get-acad-object))))
Title: Re: (and (or *acad*
Post by: Mark on August 30, 2011, 01:20:46 PM
Another might be:

Code: [Select]
(if (null *acad*)
    (setq *acad* (vlax-get-acad-object))
)
Now see, that's what I'm talkin 'bout. A plain ol' IF statement. That I understand!
LOL
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 01:22:45 PM
English language my butt! Someone needs to put that in a sentence for me 'cause I don't understand.

I see no English in this. :)
Code: [Select]
(setq *acadobject*
         (cond
           (*acadobject*)
           ((vlax-get-acad-object))))

"Set the variable based upon two conditions: the previous definition or the result to a call of `vlax-get-acad-object'"

*smile*
Title: Re: (and (or *acad*
Post by: cadman6735 on August 30, 2011, 01:25:00 PM
Why do you need a condition?
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 01:26:24 PM
@Lee, Mark:
I agree `that' statement reads and converts nicely but I also think that it hides the fact that a variable is being set/created (and if that variable is GLOBAL I think that should be "out front" and "highlighted" to any reader). That is why I adopted the (setq var (cond syntax a long time ago.
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 01:26:59 PM
Why do you need a condition?

Because you want to avoid having "extra" calls if necessary.
Title: Re: (and (or *acad*
Post by: BlackBox on August 30, 2011, 01:28:27 PM
If defining the variable in an AND statement at the beginning of a function (i.e., IF+AND), I like to use the OR method, whereas when nested in an 'action' statement (i.e., during an vlax-for iteration, etc.) I use a COND method as Alan has shown.

Yes, using the COND method would work in both situations, but I choose to use similar/separate syntax for my own edification when reading the code later. Personal choice on my part.

Separately, I also take the time to write a SETQ for each variable, instead of including them all in a single call - I find it easier to make quick changes, or substitution/exclusion(s) on the fly by simply commenting out a single SETQ as needed. Again, my preference only FWIW.
Title: Re: (and (or *acad*
Post by: MP on August 30, 2011, 01:33:19 PM
All the COND structure is doing is (represented by equiv IF code) is:

Code: [Select]
(setq *acad*
    (if *acad*
        *acad*
        (vlax-get-acad-object)
    )
)

Versus:

Code: [Select]
(setq *acad*
    (cond
        (*acad*)
        ((vlax-get-acad-object))
    )
)

and since the latter really affords no increased value (execution, "calls" or typing) I prefer the former (even tho many times I've used the latter in the past) simply because I don't have to explain it to others reading my code. Saves them and me time. /2¢

PS: If you really want to split hairs the IF code is about 10% faster than the equivalent COND code. :P
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 01:36:02 PM
Found the writeup I did and posted.

Link: http://www.theswamp.org/index.php?topic=28433.msg340206#msg340206
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 01:37:13 PM
...
PS: If you really want to split hairs the IF code is about 10% faster than the equivalent COND code. :P

Interesting.
Title: Re: (and (or *acad*
Post by: Mark on August 30, 2011, 01:39:15 PM
...
PS: If you really want to split hairs the IF code is about 10% faster than the equivalent COND code. :P

Interesting.

Logical ...


*grin*
Title: Re: (and (or *acad*
Post by: BlackBox on August 30, 2011, 01:39:59 PM
Code: [Select]
(defun setq->cond ()
  (setq *acadobject*
         (cond
           (*acadobject*)
           ((vlax-get-acad-object)))))

(defun cond->setq ()
  (cond
    (*acadobject*)
    ((setq *acadobject* (vlax-get-acad-object)))))

(defun setq->if  ()
  (setq *acadobject*
         (if *acadobject*
           *acadobject*
           (vlax-get-acad-object))))

(setq *acadobject* nil)

(bench '(setq->cond) '() 10000000)

(setq *acadobject* nil)

(bench '(cond->setq) '() 10000000)

(setq *acadobject* nil)

(bench '(setq->if) '() 10000000)

Results:

Code: [Select]
SETQ->COND
Elapsed: 31918
Average: 0.0032

COND->SETQ
Elapsed: 33150
Average: 0.0033

Command: 'VLIDE
SETQ->IF
Elapsed: 31029
Average: 0.0031

Edit: Not quite 10% faster (as advertised), rather only +/-1% on average over 10000000 iterations. LoL

Edit: CORRECTION, my fat fingers typed incorrectly... (* 100.0 (/ (- 33150 31029) 33150.0)) is +/-6%. My bad.
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 01:42:32 PM
...
PS: If you really want to split hairs the IF code is about 10% faster than the equivalent COND code. :P
Interesting.
Logical ...
*grin*

Are you sure you don't have anywhere else to be?

*bigger grin*
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 01:44:22 PM
Thanks for assembling that test RenderMan.
Title: Re: (and (or *acad*
Post by: Mark on August 30, 2011, 01:45:50 PM
Are you sure you don't have anywhere else to be?
*bigger grin*
You're right, I hear the Lagniappe forum calling me ...

get outta here ya crazy coders

LOL
Title: Re: (and (or *acad*
Post by: MP on August 30, 2011, 01:46:03 PM
Edit: Not quite 10% faster (as advertised), rather only +/-1% on average over 10000000 iterations. LoL

(http://cache.ohinternet.com/images/b/b1/Doing-it-wrong.jpg)

Code: [Select]
Elapsed milliseconds / relative speed for 65536 iteration(s):

    (SETQ *ACAD* (IF *ACAD* *ACAD* (vlax...).....1375 / 1.10 <fastest>
    (SETQ *ACAD* (COND (*ACAD*) ((vlax-g...).....1515 / 1.00 <slowest>
Title: Re: (and (or *acad*
Post by: BlackBox on August 30, 2011, 01:47:30 PM
Found the writeup I did and posted.

Link: http://www.theswamp.org/index.php?topic=28433.msg340206#msg340206

... Only skimmed, but looks 'neat' (yes, 'cause I'm a geek)

Thanks for assembling that test RenderMan.

'Welcome; interesting that the write up sites Robert Bell, who wrote the Bench.lsp I used for the test! LoL

Title: Re: (and (or *acad*
Post by: BlackBox on August 30, 2011, 01:50:33 PM
Edit: ^^ LoL

Edit: Not quite 10% faster (as advertised), rather only +/-1% on average over 10000000 iterations. LoL

(http://cache.ohinternet.com/images/b/b1/Doing-it-wrong.jpg)

Code: [Select]
Elapsed milliseconds / relative speed for 65536 iteration(s):

    (SETQ *ACAD* (IF *ACAD* *ACAD* (vlax...).....1375 / 1.10 <fastest>
    (SETQ *ACAD* (COND (*ACAD*) ((vlax-g...).....1515 / 1.00 <slowest>

Show me the 10%....

<snip>

Results:

Code: [Select]
SETQ->COND
Elapsed: 31918
Average: 0.0032

COND->SETQ
Elapsed: 33150
Average: 0.0033

Command: 'VLIDE
SETQ->IF
Elapsed: 31029
Average: 0.0031

Edit: Not quite 10% faster (as advertised), rather only +/-1% on average over 10000000 iterations. LoL
Title: Re: (and (or *acad*
Post by: MP on August 30, 2011, 01:52:54 PM
Show me the 10%....

Sorry, can't help you with your math skills. (http://www.theswamp.org/screens/mp/poke.gif)
Title: Re: (and (or *acad*
Post by: Mark on August 30, 2011, 01:54:21 PM
The 'Visual' in AutoLISP (Vanilla / Visual) doesn't mean that.
Title: Re: (and (or *acad*
Post by: BlackBox on August 30, 2011, 01:55:08 PM
Show me the 10%....

Sorry, can't help you with your math skills. (http://www.theswamp.org/screens/mp/poke.gif)

Yeah... Guess you'd require having some math skills, to do that, huh?  :-D
Title: Re: (and (or *acad*
Post by: alanjt on August 30, 2011, 01:55:38 PM
(http://www.theswamp.org/lilly_pond/alanjt/popcorn_small.gif)
Title: Re: (and (or *acad*
Post by: Mark on August 30, 2011, 01:57:36 PM
(http://www.theswamp.org/screens/mark/trolling_2.jpg)
Title: Re: (and (or *acad*
Post by: Maverick® on August 30, 2011, 02:00:12 PM
You people are such geeks.  :lmao:

(meant in the most respectful way *except Se7en*)

edited: Clara Fication made me do it
Title: Re: (and (or *acad*
Post by: BlackBox on August 30, 2011, 02:01:53 PM
You people are such geeks.  :lmao:

(meant in the most respectful way)

Following the existing theme... *tips hat*

(http://theearmark.com/wp-content/uploads/2009/08/bush-cowboy-hat-tip-231x300.jpg)
Title: Re: (and (or *acad*
Post by: Mark on August 30, 2011, 02:17:36 PM
Just Dropped In To See What Condition My Condition Was In (http://en.wikipedia.org/wiki/Just_Dropped_In_(To_See_What_Condition_My_Condition_Was_In))
:)
Title: Re: (and (or *acad*
Post by: alanjt on August 30, 2011, 02:18:39 PM
Just Dropped In To See What Condition My Condition Was In (http://en.wikipedia.org/wiki/Just_Dropped_In_(To_See_What_Condition_My_Condition_Was_In))
:)
(http://www.theswamp.org/lilly_pond/alanjt/lmao.gif)
Title: Re: (and (or *acad*
Post by: BlackBox on August 30, 2011, 02:19:16 PM
So, cadman6735... Did you get all of that?  :-D LoL
Title: Re: (and (or *acad*
Post by: MP on August 30, 2011, 02:23:49 PM
Yeah... Guess you'd require having some math skills, to do that, huh?  :-D

(http://images.icanhascheezburger.com/completestore/2008/12/28/128749471962262141.jpg)

Dude, even your data demonstrates more than a 6% improvement:

(* 100.0 (/ (- 33150 31029) 33150.0))

/lolololololol
Title: Re: (and (or *acad*
Post by: alanjt on August 30, 2011, 02:25:32 PM
*cough*you got served*cough*
Title: Re: (and (or *acad*
Post by: BlackBox on August 30, 2011, 02:27:49 PM
Fine... and the 10% would be where?

Are we rounding 6-->10 now?
Title: Re: (and (or *acad*
Post by: MP on August 30, 2011, 02:30:19 PM
Fine... and the 10% would be where?

Are we rounding 6-->10 now?

(http://www.theswamp.org/screens/mp/facepalm.gif)

(* 100.0 (/ (- 1515 1375) 1515.0)) => 9.2

*bazinga*

Title: Re: (and (or *acad*
Post by: BlackBox on August 30, 2011, 02:32:40 PM
Code: [Select]
Command: (* 100.0 (/ (- 33150 31029) 33150.0))
6.39819

 :-D


(vl-string-subst "D" "T" "Touché")
Title: Re: (and (or *acad*
Post by: BlackBox on August 30, 2011, 02:43:25 PM
Well, *I got served* ... on to my next embarrassing adventure.  :lol:

Edit:

(http://www.geekforcefive.com/images/uploads/stormtrooper-regrets.jpg)
Title: Re: (and (or *acad*
Post by: cadman6735 on August 30, 2011, 02:59:19 PM
Quote
So, cadman6735... Did you get all of that?  LoL

I learn just as much from you guys bantering as I do asking the question, never know which direction you guys will go.  But someone in here said you guys are a bunch of geeks, I agree with them...  LOL

you guys are awsome, thanks for the help.
Title: Re: (and (or *acad*
Post by: Maverick® on August 30, 2011, 04:26:56 PM
  But someone in here said you guys are a bunch of geeks, I agree with them...  LOL


Giggity
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 05:10:38 PM
I've heard the phrase "A picture is worth a 1000 words" but you guys take it to a whole new level. As far as I'm concerned you guys can stop having these "picture conversations" at any time.

I don't recall off hand when in human development cycle language processing and use happens but I'm sure it's early ("words be tough" I guess).








:P
Title: Re: (and (or *acad*
Post by: BlackBox on August 30, 2011, 05:13:23 PM
I've heard the phrase "A picture is worth a 1000 words" but you guys take it to a whole new level. As far as I'm concerned you guys can stop having these "picture conversations" at any time.

I don't recall off hand when in human development cycle language processing and use happens but I'm sure it's early ("words be tough" I guess).








:P

Gadzooks! Mark the calendar - Se7en used a smiley!!!  :-D LoL
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 05:21:23 PM
Gadzooks! Mark the calendar - Se7en used a smiley!!!  :-D LoL

No, I used a colon and a `P'.


Colon Dash `D'
Title: Re: (and (or *acad*
Post by: alanjt on August 30, 2011, 05:23:57 PM
(http://www.theswamp.org/lilly_pond/alanjt/yawn.gif)
Title: Re: (and (or *acad*
Post by: MP on August 30, 2011, 05:32:54 PM
As far as I'm concerned you guys can stop having these "picture conversations" at any time.

(http://troll.me/images/pissed-off-obama/your-roleknow-it-your-mouthshut-it.jpg)
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 05:33:18 PM
(http://www.theswamp.org/lilly_pond/alanjt/yawn.gif)

What is that? ...a reenactment of your Hanson ("MMMBop") groupie days?
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 05:35:51 PM
As far as I'm concerned you guys can stop having these "picture conversations" at any time.
...

Hey, you Canadians-ites-ers can't use photos of our people. Go find a Deline Dion photo to edit.
Title: Re: (and (or *acad*
Post by: MP on August 30, 2011, 05:37:30 PM
I was born in 'merka, accordingly, I does what I wants.  :-P
Title: Re: (and (or *acad*
Post by: Maverick® on August 30, 2011, 05:41:01 PM
No, I used a colon and a `P'.



Most of us already knew your colon was attached to your communication center.
Title: Re: (and (or *acad*
Post by: alanjt on August 30, 2011, 05:48:15 PM
(http://www.theswamp.org/lilly_pond/alanjt/yawn.gif)

What is that? ...a reenactment of your Hanson ("MMMBop") groupie days?
you must be a member too.
Title: Re: (and (or *acad*
Post by: Maverick® on August 30, 2011, 05:49:49 PM
you must be a member

Oh, no doubt.   :lmao:
Title: Re: (and (or *acad*
Post by: alanjt on August 30, 2011, 05:50:55 PM
LOL
Title: Re: (and (or *acad*
Post by: Maverick® on August 30, 2011, 05:54:41 PM
Mark's gonna lock me outa the "real" forums.  :oops:  :-)
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 06:49:57 PM
Mark's gonna lock me outa the "real" forums.  :oops:  :-)
Yeah! I will try to act hurt when the announcement is made.

BTW, do you know if there is some sort of VOTE or anyway we can move up the effective date to something like yesterday (any link appreciated)?
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 06:52:31 PM
(http://www.theswamp.org/lilly_pond/alanjt/yawn.gif)
What is that? ...a reenactment of your Hanson ("MMMBop") groupie days?
you must be a member too.

*face-palm* Next time you think up a good "come-back"; please keep it to yourself.
Title: Re: (and (or *acad*
Post by: alanjt on August 30, 2011, 06:55:58 PM
(http://www.theswamp.org/lilly_pond/alanjt/yawn.gif)
What is that? ...a reenactment of your Hanson ("MMMBop") groupie days?
you must be a member too.

*face-palm* Next time you think up a good "come-back"; please keep it to yourself.
It must be lonely, sitting on that pedestal, all by yourself.
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 06:59:37 PM
*lmao* See, there you go!

***

Good idea! I wonder if Mark would create a sub-forum where I was the only member. ...I'm sure (like 80% sure) that the conversations would be epically awesome.
Title: Re: (and (or *acad*
Post by: alanjt on August 30, 2011, 07:01:56 PM
*lmao* See, there you go!

***

Good idea! I wonder if Mark would create a sub-forum where I was the only member. ...I'm sure (like 80% sure) that the conversations would be epically awesome.
Why wait, just start emailing yourself. : P
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 07:06:37 PM
...
Good idea! I wonder if Mark would create a sub-forum where I was the only member. ...I'm sure (like 80% sure) that the conversations would be epically awesome.
Why wait, just start emailing yourself. : P

You're a genius! "private mailing list"! ...Winning!
Title: Re: (and (or *acad*
Post by: alanjt on August 30, 2011, 07:10:15 PM
...
Good idea! I wonder if Mark would create a sub-forum where I was the only member. ...I'm sure (like 80% sure) that the conversations would be epically awesome.
Why wait, just start emailing yourself. : P

You're a genius! "private mailing list"! ...Winning!
I like to keep it simple. Easier on the brain.
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 30, 2011, 07:13:52 PM
...
Good idea! I wonder if Mark would create a sub-forum where I was the only member. ...I'm sure (like 80% sure) that the conversations would be epically awesome.
Why wait, just start emailing yourself. : P
You're a genius! "private mailing list"! ...Winning!
I like to keep it simple. Easier on the brain.

No, yeah I know...your code demonstrates that point perfectly.
Title: Re: (and (or *acad*
Post by: alanjt on August 30, 2011, 07:15:48 PM
...
Good idea! I wonder if Mark would create a sub-forum where I was the only member. ...I'm sure (like 80% sure) that the conversations would be epically awesome.
Why wait, just start emailing yourself. : P
You're a genius! "private mailing list"! ...Winning!
I like to keep it simple. Easier on the brain.

No, yeah I know...your code demonstrates that point perfectly.
I do what I can to ensure you understand what I keep between my parens.
Title: Re: (and (or *acad*
Post by: MP on August 30, 2011, 08:11:20 PM
I wonder if Mark would create a sub-forum where I was the only member.

(http://i52.tinypic.com/2ik884y.jpg)
Title: Re: (and (or *acad*
Post by: kdub on August 30, 2011, 11:50:25 PM

now that the technical conversation is complete :

wasn't there a discussion that included T-Shirt slogans and had a definitive resolution to this topic ??
Title: Re: (and (or *acad*
Post by: John Kaul (Se7en) on August 31, 2011, 12:12:24 PM
now that the technical conversation is complete :
wasn't there a discussion that included T-Shirt slogans and had a definitive resolution to this topic ??

I don't know if there was a `resolution' so-to-speak but I do seem to recall being called a few names, generally picked on and abused though. *sniff* *sob* ...I still wake in the middle of the night sometimes.

However, if we are voting on a `resolution' I vote we ship MP back to the 1980's (he be mean).

***

Pertaining to the "COND is ~10% slower" issue. I don't recall but would making a DEFUN speed up the process a bit (I doubt this will work but I just wanted to offer up something to see if there was any extra information you guys failed to give up).

For example: I remembered--and found--that goofy AIF function I made.
Code: [Select]
(defun aif ( var expr iffalse )
    ;; anaphoric-if
    ;; EX:
    ;;     (aif 'a (getint "\nEnter new value [1]: ") 1)
    ;;     (aif 'a a 2)
    ;;
    (set var (cond (expr) (iffalse))) )
Title: Re: (and (or *acad*
Post by: BlackBox on August 31, 2011, 12:37:56 PM
I don't know if there was a `resolution' so-to-speak

I *believe* this was the 'resolution' to the original topic:

...
someone in here said you guys are a bunch of geeks, I agree with them...  LOL

you guys are awsome, thanks for the help.

 :-D