Author Topic: Layers frozen in viewports ...  (Read 13146 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Layers frozen in viewports ...
« Reply #15 on: February 12, 2005, 12:46:18 AM »
Ok Charles, I'm going to take a stab at this. Bear with me, I'm not the best teacher.

Ahhh lambda --

Quote from: AutoLISP Refrence
Use the lambda function when the overhead of defining a new function is not justified. It also makes the programmer's intention more apparent by laying out the function at the spot where it is to be used. This function returns the value of its last expr, and is often used in conjunction with apply and/or mapcar to perform a function on a list.

Trouble with the reference is that it shows only two examples, one for apply, and one for mapcar. It implies that's the only way it can be used.

As such, the calling syntax is the same for both, insomuch as you call lambda quoted --

Code: [Select]
(apply
   '(lambda (x y z)
        (* x (+ y z))
    )
   '(5 6 7)
)

=> 65

(mapcar
   '(lambda (x)
        (* x 25)
    )
   '(2 -4 6 -8 10)
)

=> (50 -100 150 -200 250)

Now think about this for a moment. If you were not using lambda how might you use apply and mapcar?

Couple examples --

Code: [Select]
(apply
   '+
   '(1 2 3)
)

=> 6

(mapcar
   'itoa
   '(1 2 3)
)

=> ("1" "2" "3")

Anything kind of leap off the page?

The functions passed to apply and mapcar were quoted.

<voice=BasilFawlty>Ahhh, fascinating, completely rivoting. And salient perhaps ummm, how?</voice>

When you use apply and mapcar you need to quote the functions passed to it. Subtitle: You can use lambda unquoted where a function would be used unquoted.

Consider this --

Code: [Select]
(defun squarex (x)
    (* x x)
)

To use --

(squarex 5)

=> 25

What if we didn't want the overhead of defining a function, but our code's readability (and maintainability) would be improved if we could define and use functionality precisely where we need it? Sounds like "lambda to the rescue" to me --

Code: [Select]
(   (lambda (x)
        (* x x)
    )
    5
)

=> 25

Perhaps laying out both horizontally will be more illuminating --

Code: [Select]
(squarex 5)
((lambda (x) (* x x)) 5)

Ding! Ding! Ding!

Another one to illustrate argument passing --

Code: [Select]
(   (lambda (a b c)
        (/ (+ a b) (float c))
    )
    1
    2
    4
)
=> 0.75

Did this help? Do you see now that I am not calling mapcar, explicitly or implicitly in my GetLayersFrozenInViewport function?


Perhaps you will view this function in a new light. :)

Oh, just about forgot --

Code: [Select]
(entget
    ename
   '("acad") ;; <= this tells entget to retrieve the
             ;;    extended entity data for registered
             ;;    application name "acad". This is
             ;;    where the frozen layer information
             ;;    is stored and why we wants it. :)
)

:) (my, my these are big assed icons)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Layers frozen in viewports ...
« Reply #16 on: February 12, 2005, 09:39:36 AM »
Very nice explination MP! But if i may also add my "look here" statment.

Have a look HERE and get some more information. (Note: Scheme syntax looks a bit weird but just read the descriptions and you will understand.)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Layers frozen in viewports ...
« Reply #17 on: February 12, 2005, 09:44:45 AM »
Thanks Se7en. Very similar to Pages 62 ... in "Structure and Interprettation of Computer Programs", Abelson / Sussman.

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

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Layers frozen in viewports ...
« Reply #18 on: February 12, 2005, 09:56:22 AM »
Quote from: MP
Ok Charles, I'm going to take a stab at this. Bear with me, I'm not the best teacher.

But you do a good job in my opinion.


OK, Taking it very slow for you know who.  :) [ME]

Apply passes the list as a list expecting the number of variables matches the number of items in the list.
Code: [Select]
(apply
   '(lambda (x y z) ; the 3 variables
        (* x (+ y z))
    )
   '(5 6 7) ; the list of 3 items
)

;returns one result
=> 65



Mapcar passes the list one item at a time collecting the result before passing the next item.
Then returns the individual results in a list.
Code: [Select]
(mapcar
   '(lambda (x) ; one variable 'x'
        (* x 25)
    )
   '(2 -4 6 -8 10) ; the list of 5 items
)

; returns a list of results
=> ("1" "2" "3")



In the Help the syntax given for Lambda is
Code: [Select]
(lambda arguments expr...)
but should have been ???
Code: [Select]
( [optionsl function] (lambda arguments expr...) list)
unless there is another way to use it, you must pass it something so you always need
the extra parens.

In your example
Code: [Select]
( (lambda (x) ; one variable
    (* x x)
  )
  5   ; one item, one variable
)

=> 25; one result


Could also have multiple items, but would need variables to match

Code: [Select]

((lambda (x y) ; two variables
   (* x y)
  )
  5 2   ; two items
)


So this is like the Apply in that the number of items has to match the number of variables
But the items are not in a list as ther are with Apply.


How did I do Teach?
Great graphic by the way, highlighting the data. What program did you use to produce it?

As for:
Code: [Select]
(entget
    ename
   '("acad") ;; <= this tells entget to retrieve the
             ;;    extended entity data for registered
             ;;    application name "acad". This is
             ;;    where the frozen layer information
             ;;    is stored and why we wants it. :)
)

You can use it any time you need to retrieve the x-data of an entity?
So much to learn too little time. :)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

whdjr

  • Guest
Layers frozen in viewports ...
« Reply #19 on: February 14, 2005, 03:30:56 PM »
MP,

I really have this love for vl-remove-if-not, so I modified your code :) .  Do you like?
Code: [Select]
(defun GetLayersFrozenInViewport (viewportEname)
  (mapcar 'cdr
 (vl-remove-if-not
   '(lambda (x)
      (= (car x) 1003)
    )
   (cdadr
     (assoc -3
    (entget
      viewportEname
      '("acad")
    )
     )
   )
 )
  )
)

I hope I don't get in trouble with the copyright police.  :shock:

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Layers frozen in viewports ...
« Reply #20 on: February 14, 2005, 04:03:27 PM »
Do I like? Not really, it executes about 30% slower than going my route.

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

whdjr

  • Guest
Layers frozen in viewports ...
« Reply #21 on: February 14, 2005, 04:22:02 PM »
Using your tool this is what I get:
Code: [Select]
Benchmarking ......................Elapsed milliseconds / relative speed for 524288 iteration(s):

    GETLAYERSFROZENINVIEWPORT.....1921 / 1.00 <fastest>
    TEST..........................1922 / 1.00 <slowest>

Where is the 30%?

MP (not logged in)

  • Guest
Layers frozen in viewports ...
« Reply #22 on: February 14, 2005, 04:28:44 PM »
Apparently mileage varies.

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

    (GETLAYERSFROZENINVIEWPORT1 ENAME).....1219 / 1.36 <fastest>
    (GETLAYERSFROZENINVIEWPORT2 ENAME).....1656 / 1.00 <slowest>

Subtitle: Probably better to use vl-remove-if/not unless hand rolled code can definately out perform native functions.

MP (not logged in)

  • Guest
Layers frozen in viewports ...
« Reply #23 on: February 14, 2005, 04:29:59 PM »
524288 iterations?

Yeow, what are you running, a CRAY?

whdjr

  • Guest
Layers frozen in viewports ...
« Reply #24 on: February 14, 2005, 04:33:05 PM »
I ran it a few more times:

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

    GETLAYERSFROZENINVIEWPORT.....1921 / 1.00 <fastest>
    TEST..........................1922 / 1.00 <slowest>

Elapsed milliseconds / relative speed for 131072 iteration(s):

    GETLAYERSFROZENINVIEWPORT.....1266 / 1.10 <fastest>
    TEST..........................1391 / 1.00 <slowest>

Elapsed milliseconds / relative speed for 131072 iteration(s):

    TEST..........................1281 / 1.00 <fastest>
    GETLAYERSFROZENINVIEWPORT.....1282 / 1.00 <slowest>

Elapsed milliseconds / relative speed for 131072 iteration(s):

    TEST..........................1078 / 1.06 <fastest>
    GETLAYERSFROZENINVIEWPORT.....1140 / 1.00 <slowest>

Elapsed milliseconds / relative speed for 131072 iteration(s):

    GETLAYERSFROZENINVIEWPORT.....1500 / 1.06 <fastest>
    TEST..........................1594 / 1.00 <slowest>

Seems like your won all the way around.


hmm...maybe the benchmark tool is weighted....?  :?

whdjr

  • Guest
Layers frozen in viewports ...
« Reply #25 on: February 14, 2005, 04:36:39 PM »
Quote from: MP (not logged in)
524288 iterations?

Yeow, what are you running, a CRAY?


Just the tool I copied from here

MP (not logged in)

  • Guest
Layers frozen in viewports ...
« Reply #26 on: February 14, 2005, 05:23:09 PM »
Guess I should have mentioned the bias coded into BenchMark. :)