Author Topic: There's got to be a better way  (Read 10663 times)

0 Members and 1 Guest are viewing this topic.

CADaver

  • Guest
Re: There's got to be a better way
« Reply #15 on: April 06, 2006, 08:38:10 AM »
Okay guys, remember us slow folks are still around, and had I known I'd have needed those brain cells, I'd have been kinder to 'em in my youth.  Michael, when you have time, could you spread out FORFUN with a little explanation for me.  I'm trying to reach around it and get a grip on it for application.  :|

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: There's got to be a better way
« Reply #16 on: April 06, 2006, 09:20:22 AM »
Does this help?

Code: [Select]
(defun ForFun ( / data result )
    (while (setq data (tblnext "layer" (null data)))
      ;; tblnext when used this way, with the `data' var not valid,
      ;; {the var has not been initialized yet so `(nul nil) will return `T}
      ;; will retrieve the first entity in the database.
      ;; so this loop will continue thru the entire dwg database.
        (setq result
            (cons
                (mapcar
                  '(lambda (key) (cdr (assoc key data)))
                  '(2 62 6)
                )
                result
            )
        )
    )
)

Does that help?
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.
Re: There's got to be a better way
« Reply #17 on: April 06, 2006, 09:42:53 AM »
How about this Chuck, does this illuminate the underpinnings?

Code: [Select]
(defun c:sample ( / foo data keys )

    (defun foo ( data keys / result )

        ;;  the core algorythm
       
        (setq result
            (mapcar
               '(lambda (key) (cdr (assoc key data)))
                keys
            )
        )
       
        ;;  print out the original data
       
        (princ
            (strcat
                "Data = "
                (vl-prin1-to-string data)
                "\n"
            )
        )
       
        ;;  print out the keys used to filter the data
   
        (princ
            (strcat
                "Keys = "
                (vl-prin1-to-string keys)
                "\n"
            )
        )
       
        ;;  print out the filtered data
   
        (princ
            (strcat
                "Filtered data = "
                (vl-prin1-to-string result)
                "\n\n"
            )
        )
       
    )
   
    ;;  set up some simple data
   
    (setq data
       '(
            (1 . "Chuck")
            (2 . "John")
            (3 . "Mark")
            (4 . "Michael")
            (5 . "Randy")
        )
    )
   
    ;;  now filter using different keys
   
    (foo data '(1))
   
    (foo data '(1 2 3))
   
    (foo data '(1 3 5))
   
    (princ)

)

Output --

Code: [Select]
Data = ((1 . "Chuck") (2 . "John") (3 . "Mark") (4 . "Michael") (5 . "Randy"))
Keys = (1)
Filtered data = ("Chuck")

Data = ((1 . "Chuck") (2 . "John") (3 . "Mark") (4 . "Michael") (5 . "Randy"))
Keys = (1 2 3)
Filtered data = ("Chuck" "John" "Mark")

Data = ((1 . "Chuck") (2 . "John") (3 . "Mark") (4 . "Michael") (5 . "Randy"))
Keys = (1 3 5)
Filtered data = ("Chuck" "Mark" "Randy")
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
Re: There's got to be a better way
« Reply #18 on: April 06, 2006, 10:04:38 AM »
Well I'm too slow as usual, but here is my expiation attempt.


Code: [Select]
(defun ForFun ( / data result )
  ;;  (while ..) - Returns The most recent value of the last expr. therefore in this case
  ;;  it will return the 'result'
           ;;  (tblnext table-name [rewind]) - get the next related item in the table
           ;;  [rewind] If present and not nil, the first entry in it is retrieved
           ;;   in this case (null data) returns T the first time through thus causing
           ;;   the first record to be returned, thereafter it is nil & steps through the table
    (while (setq data (tblnext "layer" (null data)))
        (setq result  ;  collect the result each time through the loop
            (cons        ;  (cons new-first-element list-or-atom)  make a list
                         ;   because 'result' is nil the first time through and nil is considered
                         ;   a list, 'setq result' gets the first list from the mapcar and makes
                         ;   it a list,  ((<mapcar output>)) on subsequent loops it becomes
                         ;   ((<mapcar output3>)(<mapcar output2>)(<mapcar output1>))
                (mapcar     ;  Returns a list of the result of executing a function with the individual elements of a list
                            ;  In this case each item from the list (2 62 6) is fed to the lambda one at a time
                            ;  first 2, then 62 and finally 6
                 
                   '(lambda (key)    ;  Defines an anonymous function
                       
                      (cdr (assoc key data)) ;  This function you know, assoc returns the list or pair if a match is found
                                             ;   if 'data' is ((0 . "LAYER") (2 . "0") (70 . 0) (62 . 7) (6 . "Continuous"))
                                             ;   (assoc 2  data )  returns (2 . "0") which is layer "0"
                                             ;   (cdr (2 . "0"))  returns  "0"
                    )
                   
                   '(2 62 6)  ;  this is the list in the mapcar statement
                )
                result    ; this the list in the cons statement
            )
        )
    )
)
<spelling>
« Last Edit: April 06, 2006, 12:21:03 PM by CAB »
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.

CADaver

  • Guest
Re: There's got to be a better way
« Reply #19 on: April 06, 2006, 12:12:27 PM »
whoa!  I learned more from the last three posts than I have in the last year... now I need a nap.  :lol:

Realizing that the data var was not valid returning a T, was like "Well of course why didn't I know that". :ugly:  And it took me a minute to get around the (mapcar (lambda to pull the layer, color and linetype (2 62 6), but I think I got it.

KEWL, Thanks guys.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: There's got to be a better way
« Reply #20 on: April 06, 2006, 12:58:19 PM »
Mapcar-lambda is easy. Here, watch:

Mapcar: Do <something> to every item in a list.

Lambda: Same as defun but no `name' ...A Lambda is list of process' just like a formal(named) procedure.

Demonstrations:

Mapcar can be remade like this (For the sake of this convo just conern your self with the basics.):

Demonstration will demonstrate the process of printing each item in a list.

Code: [Select]
(defun print-it (lst)
   (if (null lst)
   ;; if the list is `empty' or `nil'
     nil
   ;; return `nil'
     (progn
   ;; otherwise
        (princ (car lst))
   ;; print the first item in the list
        (print-it (cdr lst))
   ;; strip off the first item(cause we just printed it) and go thur again.
        )
     )
   )
lambda is even easier to unerstand. Since it is nothing more then a formal procedure without a name it can be defined on the fly(so to speak) or at the point of use/need.

Code: [Select]
( (lambda () (1+ 2)) )
Will return what? ...3. Good.

So, a Mapcar-Lambda statment is saying nothing more then: "Preform <this> function on each and every item in a list."

How's that? Is the process a bit more clear?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CADaver

  • Guest
Re: There's got to be a better way
« Reply #21 on: April 06, 2006, 01:57:17 PM »
Got it, thanks.  I now have a whole new realm of applications buzzing in me punkin' head.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: There's got to be a better way
« Reply #22 on: April 06, 2006, 02:21:19 PM »
Got it, thanks.  I now have a whole new realm of applications buzzing in me punkin' head.

BTDT -- I can recommend a good sleep clinic.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CADaver

  • Guest
Re: There's got to be a better way
« Reply #23 on: April 06, 2006, 02:47:24 PM »
Got it, thanks.  I now have a whole new realm of applications buzzing in me punkin' head.

BTDT -- I can recommend a good sleep clinic.
nah, at my age excitement is self-limiting, 10 minutes of thinkin', and I gotta take a nap.

I said "THINKIN'", now.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: There's got to be a better way
« Reply #24 on: April 06, 2006, 02:53:10 PM »
<mutter> preemptive <expletive> <grumble>
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

LE

  • Guest
Re: There's got to be a better way
« Reply #25 on: April 06, 2006, 03:39:47 PM »
Code: [Select]
(setq lst nil)
(while (setq item (tblnext "LAYER" (not item)))
  (setq lst (cons (list (cdadr item) (cdar (cdddr item)) (cdadr (cdddr item))) lst)))

Rev = correction, forgot the first layer... one more time.... another change made... Hey.... I think I need the new lisp book by Mark soon!!!!!
« Last Edit: April 06, 2006, 04:00:52 PM by LE »

T.Willey

  • Needs a day job
  • Posts: 5251
Re: There's got to be a better way
« Reply #26 on: April 06, 2006, 03:43:53 PM »
Code: [Select]
(setq lst nil)
(tblnext "LAYER" t)
(while (setq item (tblnext "LAYER"))
  (setq lst (cons (list (cdadr item) (cdar (cdddr item)) (cdadr (cdddr item))) lst)))
That's just evil Luis.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: There's got to be a better way
« Reply #27 on: April 06, 2006, 03:57:16 PM »
what  :evil:  :evil:  :evil:
:lmao:

At least it wasn't
Code: [Select]
(setq lst nil)
(tblnext "LAYER" t)
(while (setq item (tblnext "LAYER"))
 (setq
  lst
  (list
   (cdr
    (car
     (cdr item)
    )
   )
   (cdr
    (car
     (cdr
      (cdr
       (cdr item)
      )
     )
    )
   )
   (cdr
    (car
     (cdr
      (cdr
       (cdr
        (cdr item)
       )
      )
     )
    )
   )
  )
 )
)

ps
if that is a bad code I apologize... have been a looooooooong time, that I do not wrote anything in autolisp....  :cry:
Not at all.  Just thought it was funny, see above I did before you posted this one.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: There's got to be a better way
« Reply #28 on: April 06, 2006, 03:59:20 PM »
Oops.  Forgot the "cons" part, but you get the idea.  All in good fun.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: There's got to be a better way
« Reply #29 on: April 06, 2006, 04:08:27 PM »
I like seeing the (cadar... stuff, it just seems that people don't use it very often.  That is why I was said that you were evil (joking of course) because people don't seem to grasp it that easily.

 :angel:
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.