Author Topic: mapcar or foreach  (Read 11786 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: mapcar or foreach
« Reply #15 on: November 22, 2005, 04:22:15 PM »
[sidebar]
MP, it's rather ironic that you used 'ssget' to show that you use 'repeat' quite often when 'foreach' and 'mapcar' can't get 'ssget' entities.
[/sidebar]

In the spirit of fun ...

Code: [Select]
(defun PicksetToEnames ( ss / i result )
    (if (eq 'pickset (type ss))
        (repeat (setq i (sslength ss))
            (setq result
                (cons
                    (ssname ss (setq i (1- i)))
                    result
                )
            )
        )
    )
)

:)
« Last Edit: November 22, 2005, 05:55:52 PM by MP »
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: mapcar or foreach
« Reply #16 on: November 22, 2005, 06:43:15 PM »
And more fun.
Well you used the "can't" word. he he he
Code: [Select]
;;  CAB 03/22/2005
;;  just an exersize
(defun c:TxtC (/ ss elst)
  (prompt "\nSelect text objects... <exit> ")
  (and (setq ss (ssget '((0 . "TEXT") (72 . 1) (73 . 2))))
       (repeat (sslength ss)
         (setq elst (entget (ssname ss 0)))
         (entmake (list '(0 . "CIRCLE")
                        (cons 10 (cdr (assoc 11 elst)))
                        (cons 40 (* (cdr (assoc 40 elst)) 0.06))
                  )
         )
         (ssdel (ssname ss 0) ss)
       )
  )
  (princ)
)
« Last Edit: April 01, 2010, 06:56:59 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.

LE

  • Guest
Re: mapcar or foreach
« Reply #17 on: November 22, 2005, 07:03:14 PM »
Here are some samples using mapcar and foreach:

Code: [Select]
(defun rcmd-divide-vector  (pt1 pt2 n)
  (mapcar '+
  pt1
  (mapcar '/
  (mapcar '- pt2 pt1)
  (list n n n))))

(defun rcmd-transpts  (lst pt / p0 p1 ang pin ang pt1 lst1)
  (setq p0  (car lst)
p1  (cadr lst)
ang (angle p0 p1)
pin (inters p0 p1 pt (polar pt (+ ang (/ pi 2)) 1) nil)
ang (angle pin pt))
  (foreach
p
  lst
    (setq pt1  (polar p ang (distance pt pin))
  lst1 (append lst1 (list pt1))))
  lst1)

(defun rcmd-point-member  (p lst / tst)
  (foreach
pt
   lst
    (if pt
      (if (equal (distance p pt) 0 0.00001)
(setq tst T))))
  tst)

(defun rcmd-list->ss  (lst / ss0)
  (setq ss0 (ssadd))
  (foreach n lst (ssadd n ss0))
  ss0)

(defun rcmd-point-member  (p lst / tst)
  (foreach
pt
   lst
    (if pt
      (if (equal (distance p pt) 0 0.00001)
(setq tst T))))
  tst)

(defun rcmd-no-equal-points  (lst / lst2)
  (foreach
pt
   lst
    (if (not (rcmd-point-member pt lst2))
      (setq lst2 (append lst2 (list pt)))))
  lst2)

LE

  • Guest
Re: mapcar or foreach
« Reply #18 on: November 22, 2005, 07:21:08 PM »
another one:

Code: [Select]
;; string_lst = list of strings
;; flag = max or' min
(defun strlenmm (string_lst flag / lst)
  (setq lst (mapcar (function (lambda (x) (cons (strlen x) x)))
    string_lst))
  (cdr (assoc (apply 'flag (mapcar 'car lst)) lst)))

_$ (strlenmm (list "10000.0" "1.0" "2300000000" "1.234") min)
"1.0"
_$
_$ (strlenmm (list "10000.0" "1.0" "2300000000" "1.234") max)
"2300000000"
_$

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: mapcar or foreach
« Reply #19 on: November 22, 2005, 07:30:52 PM »
Will you used the "can't" word. he he he
Maybe I misread that, but I thought he was referring to the fact you can't do
Code: [Select]
(foreach ent ss ....)
(mapcar 'entget ss)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: mapcar or foreach
« Reply #20 on: November 22, 2005, 07:44:25 PM »
That's the way I read it too Jeff, ergo ..

(foreach ename (PicksetToEnames (ssget "x"))
    (foo ename)
)


Or ...

(mapcar 'foo (PicksetToEnames (ssget "x")))

:)
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: mapcar or foreach
« Reply #21 on: November 22, 2005, 08:44:56 PM »
OK, i understand now, Little slow on the uptake lately. :lol:

And this doesn't count, right?
Code: [Select]
(foreach ename (mapcar (function cadr) (ssnamex ss))
Well I tried, :-o
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.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: mapcar or foreach
« Reply #22 on: November 22, 2005, 08:54:33 PM »
Hi CAB,

In your c:TxtC, I found it interesting that you progressively popped the first item from the selection set
at each iteration and called (entget (ssname ss 0)))
rather than create an incrementing counter as an index into a static selectionset. 
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: mapcar or foreach
« Reply #23 on: November 22, 2005, 09:27:44 PM »
Thanks for noticing.
That was the deference I was trying to show from Michael's example above it.
I should have dressed it up a bit as pretty does not come naturally to me as you know.
It was an exercise in ways to iterate through a selection set and that was one of them.
As you know deleting from a selection set if you are not careful can produce unwanted results.
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
Re: mapcar or foreach
« Reply #24 on: November 23, 2005, 09:20:56 AM »
Will you used the "can't" word. he he he
Maybe I misread that, but I thought he was referring to the fact you can't do
Code: [Select]
(foreach ent ss ....)
(mapcar 'entget ss)


Thanks for helping me get my point across Jeff.

I thought I was gonna have to clarify myself.

 :-)

whdjr

  • Guest
Re: mapcar or foreach
« Reply #25 on: November 23, 2005, 09:35:28 AM »
Quote

In the spirit of fun ...

Code: [Select]
(defun PicksetToEnames ( ss / i result )
    (if (eq 'pickset (type ss))
        (repeat (setq i (sslength ss))
            (setq result
                (cons
                    (ssname ss (setq i (1- i)))
                    result
                )
            )
        )
    )
)

:)
That's the way I read it too Jeff, ergo ..

(foreach ename (PicksetToEnames (ssget "x"))
    (foo ename)
)


Or ...

(mapcar 'foo (PicksetToEnames (ssget "x")))

:)

Michael,

This just further illustrates my point that you can't get at the entities in a 'ssget' selection set.

I just thought it was funny that LE's statement that 'repeat' seems to be forgotten by most people was responded by you with an example of a situation of where the only way to get at the data in the example was to use repeat- which was the only method of the 3 previously mentioned methods that would have worked in this occurance.

 :-)

LE

  • Guest
Re: mapcar or foreach
« Reply #26 on: November 23, 2005, 09:46:44 AM »
Code: [Select]
(setq ss (ssget))
(vlax-for
       item
      (vla-get-activeselectionset
(vla-get-activedocument (vlax-get-acad-object)))
  (print item))

LE

  • Guest
Re: mapcar or foreach
« Reply #27 on: November 23, 2005, 09:51:04 AM »
Code: [Select]
(vlax-for
       item
      (vla-get-activeselectionset
(vla-get-activedocument (vlax-get-acad-object)))
  (print (entget (vlax-vla-object->ename item))))

Fatty

  • Guest
Re: mapcar or foreach
« Reply #28 on: November 23, 2005, 10:05:39 AM »
Code: [Select]
(setq ss (ssget))
(vlax-for
       item
      (vla-get-activeselectionset
(vla-get-activedocument (vlax-get-acad-object)))
  (print item))
I use the following one without declaring of ss:
Code: [Select]
(ssget)
(vlax-for
       item
      (vla-get-activeselectionset
(vla-get-activedocument (vlax-get-acad-object)))
  (print item))

LE

  • Guest
Re: mapcar or foreach
« Reply #29 on: November 23, 2005, 10:13:58 AM »
I use the following one without declaring of ss:
Code: [Select]
(ssget)

Quote

Yep... we don't need the ss var..... I am doing this samples on the fly [like mickey-mouse code]....  :-)

Thanks.