Author Topic: Tell me why I did it like this. :)  (Read 19014 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Tell me why I did it like this. :)
« on: September 23, 2010, 03:34:46 PM »
Code: [Select]
(defun GetAlignment ( value / lst )

    (setq lst
        (mapcar
           '(lambda ( enum ) (cons (eval enum) (substr (vl-symbol-name enum) 12)))
           '(   acAlignmentLeft
                acAlignmentCenter
                acAlignmentRight
                acAlignmentAligned
                acAlignmentMiddle
                acAlignmentFit
                acAlignmentTopLeft
                acAlignmentTopCenter
                acAlignmentTopRight
                acAlignmentMiddleLeft
                acAlignmentMiddleCenter
                acAlignmentMiddleRight
                acAlignmentBottomLeft
                acAlignmentBottomCenter
                acAlignmentBottomRight
            )
        )
    )

    (eval
        (cons 'defun
            (list 'GetAlignment '( value )
                (cons 'cdr
                    (list
                        (list 'assoc 'value
                            (list 'quote lst)
                        )
                    )
                )    
            )
        )
    )    
    
    (cdr (assoc value lst))
        
)

(GetAlignment 1)

>> "Center"

Later. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Tell me why I did it like this. :)
« Reply #1 on: September 23, 2010, 03:44:57 PM »
I can only think of obfuscation,
You didn't want strings in the compiled code.



« Last Edit: September 23, 2010, 03:49:13 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Tell me why I did it like this. :)
« Reply #2 on: September 23, 2010, 03:47:00 PM »
Nice performance enhancement after first call - similar to your recursive double-defun trick with the vl-remove-if...

Perhaps might improve readability to use defun-q  ?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Tell me why I did it like this. :)
« Reply #3 on: September 23, 2010, 03:53:39 PM »

Lee, if it was for performance I think just building the associative list in the initial definition would be better.


I've changed my vote.
It's to encourage posts of more than 4 characters long :)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

LE3

  • Guest
Re: Tell me why I did it like this. :)
« Reply #4 on: September 23, 2010, 03:54:31 PM »
No se (means I do not know)

Have a look at this:
_$ (GetAlignment 0)
"LEFT"
_$ (GetAlignment -0)
"LEFT"
_$ (GetAlignment -00)
"LEFT"
_$ (GetAlignment -000000000000)
"LEFT"

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Tell me why I did it like this. :)
« Reply #5 on: September 23, 2010, 04:17:22 PM »

Lee, if it was for performance I think just building the associative list in the initial definition would be better.

Ok, I change my reason: because you can. Next question.  :-D


alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Tell me why I did it like this. :)
« Reply #6 on: September 23, 2010, 04:17:51 PM »
After the first execution, the routine is rewritten with the dotted pair already constructed. From there, assoc from a list is a lot faster than stepping through it with vl-remove-if-not. Am I close?

Seems really clever, I would have just done it this way...

Code: [Select]
(defun foo (v)
  (if (and (numberp v) (<= 0 (setq v (fix v)) 14))
    (substr (vl-symbol-name
              (car (vl-remove-if-not
                     (function (lambda (e) (eq v (eval e))))
                     '(acAlignmentLeft acAlignmentCenter acAlignmentRight acAlignmentAligned
                       acAlignmentMiddle acAlignmentFit acAlignmentTopLeft acAlignmentTopCenter
                       acAlignmentTopRight acAlignmentMiddleLeft acAlignmentMiddleCenter
                       acAlignmentMiddleRight acAlignmentBottomLeft acAlignmentBottomCenter
                       acAlignmentBottomRight
                      )
                   )
              )
            )
            12
    )
  )
)

... but I'm dumb.

Cool stuff, gives me a lot to think about.
« Last Edit: September 23, 2010, 04:22:18 PM by alanjt »
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Tell me why I did it like this. :)
« Reply #7 on: September 23, 2010, 04:20:58 PM »
No se (means I do not know)

Have a look at this:
_$ (GetAlignment 0)
"LEFT"
_$ (GetAlignment -0)
"LEFT"
_$ (GetAlignment -00)
"LEFT"
_$ (GetAlignment -000000000000)
"LEFT"

ZOMG, look at this:

(zerop 0) >> T
(zerop -0) >> T
(zerop -00) >> T
(zerop -000) >> T

What does it mean??  :-o

It means the argument is normalized to 0 (what the heck is a negative zero) when the function call is unpacked, i.e. before it gets to the body of the function, but that's merely a guess.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Tell me why I did it like this. :)
« Reply #8 on: September 23, 2010, 04:24:44 PM »
 I did it because there is (1) no reason to recreate the list every time the function is called <i.e. evaluate / create the list once, then redefine the function with static data> (2) not palatable to me to hard code third party enum values <ok to do mine tho> (3) easy to revise the enum values if additional enum members are added down the road ... etc. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Tell me why I did it like this. :)
« Reply #9 on: September 23, 2010, 04:25:00 PM »
No se (means I do not know)

Have a look at this:
_$ (GetAlignment 0)
"LEFT"
_$ (GetAlignment -0)
"LEFT"
_$ (GetAlignment -00)
"LEFT"
_$ (GetAlignment -000000000000)
"LEFT"

ZOMG, look at this:

(zerop 0) >> T
(zerop -0) >> T
(zerop -00) >> T
(zerop -000) >> T

What does it mean??  :-o

lol spooky stuff  :lol:


JCTER

  • Guest
Re: Tell me why I did it like this. :)
« Reply #10 on: September 23, 2010, 04:28:42 PM »
I've changed my vote.
It's to encourage posts of more than 4 characters long :)
Nope

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Tell me why I did it like this. :)
« Reply #11 on: September 23, 2010, 04:32:27 PM »
I can only think of obfuscation,
You didn't want strings in the compiled code.

Truly neither -- I actually tried to pen it so it would be easy for people to see what I did. I fail.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Tell me why I did it like this. :)
« Reply #12 on: September 23, 2010, 04:33:49 PM »
I would have just done it this way...

I don't understand the benefit of that approach, but I am open to enlightenment. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Tell me why I did it like this. :)
« Reply #13 on: September 23, 2010, 04:34:04 PM »
I can only think of obfuscation,
You didn't want strings in the compiled code.
I fail.
I wouldn't say that.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Tell me why I did it like this. :)
« Reply #14 on: September 23, 2010, 04:34:50 PM »
I would have just done it this way...

I don't understand the benefit of that approach, but I am open to enlightenment. :)
I didn't say it was a benefit. I was stating that I would never have thought to create the dotted pair list and redefine the command.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox