Author Topic: Looping techniques  (Read 5612 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Looping techniques
« on: July 12, 2005, 08:16:08 AM »
Anyone want to discuss the different looping techniques, foreach, while etc.?

What are the differences between them?

When should I use 'repeat'?
When should I use 'foreach'?

etc...
TheSwamp.org  (serving the CAD community since 2003)

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Looping techniques
« Reply #1 on: July 12, 2005, 09:13:04 AM »
I would be interested in seeing the differances myself.  It would be an interesting thread.
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

whdjr

  • Guest
Looping techniques
« Reply #2 on: July 12, 2005, 09:26:02 AM »
Are we classifying mapcar, apply, vlax-for, and vlax-map-collection as looping functions as well?

whdjr

  • Guest
Re: Looping techniques
« Reply #3 on: July 12, 2005, 09:28:29 AM »
Quote from: Mark Thomas
Anyone want to discuss the different looping techniques, foreach, while etc.?

What are the differences between them?

When should I use 'repeat'?
When should I use 'foreach'?

etc...

I personally like mapcar the best (if it's considered looping); however of the two mentioned above I use repeat from time to time but I haven't used foreach in a long time.

my 2c

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Looping techniques
« Reply #4 on: July 12, 2005, 09:35:07 AM »
Quote from: whdjr
Are we classifying mapcar, apply, vlax-for, and vlax-map-collection as looping functions as well?

vlax-for definitely. I'm not sure I would consider the others as looping type functions but perhaps one of the masters will clarify this for us. :)

How about some examples?
TheSwamp.org  (serving the CAD community since 2003)

whdjr

  • Guest
Looping techniques
« Reply #5 on: July 12, 2005, 11:28:22 AM »
Well here's one for 'repeat':
Code: [Select]
(defun *ssnames* (selection_set / num lst)
  (repeat (setq num (sslength selection_set))
    (setq num (1- num)
 lst (cons (ssname selection_set num) lst)
    )
  )
  lst
)


and here's one for 'vlax-map-collection':
Code: [Select]
(defun *layout_list* (/ lst)
  (vlax-map-collection
    (vla-get-activedocument (vla-get-documents (vlax-get-acad-object)))
    '(lambda (x) (setq lst (cons x lst)))
  )
)


and here's one using 'vlax-for':
Code: [Select]
(defun *get_block_objs* (name document / blkObjlist)
  (vlax-for item (vla-get-modelspace document)
    (if (and (= (vla-get-ObjectName item) "AcDbBlockReference")
    (= (vla-get-name item) name)
)
      (setq blkObjlist (cons item blkObjlist))
    )
  )
  blkObjlist
)


Those are just a few.

whdjr

  • Guest
Looping techniques
« Reply #6 on: July 12, 2005, 11:31:19 AM »
Quote from: Mark Thomas
... I'm not sure I would consider the others as looping type functions but perhaps one of the masters will clarify this for us.

While mapcar, apply, and vlax-map-collection don't act like the traditional 'loop' functions they do inspect each item or act upon each item which would imitate a loop.

Tom Smith

  • Guest
Looping techniques
« Reply #7 on: July 12, 2005, 11:53:13 AM »
Of the simpler mechanisms in the original post, these can overlap in functionality, but the most natural usages would be ...

Repeat - when the number of iterations of the same operation is known, as in printing "hello world" five times. There's an implied counter.

Foreach - when the work to be done has some correspondence to the members of a known list. The list in effect serves as a counter.

While - when the work is to continue until a specific condition is met. Usually an explicit counter variable is set and incremented (with a cutoff value) or a flag variable is established to signal completion.

I wouldn't consider mapcar and apply to be "loop" control structures in quite the same sense, though their operation is constrained by a list.

SMadsen

  • Guest
Looping techniques
« Reply #8 on: July 12, 2005, 11:55:04 AM »
Will, APPLY merely pushes a function onto a list of arguments and forces evaluation. Although recursive for certain functions, it's not really a loop type function. E.g. for symbol type functions, you can do a simple definition like (defun myApply (fn lst)(eval (cons fn lst))).

JohnK

  • Administrator
  • Seagull
  • Posts: 10656
Looping techniques
« Reply #9 on: July 12, 2005, 12:38:58 PM »
Code: [Select]
(defun map (proc items)
  (if (null items)
    nil
    (cons (proc (car items))
          (map proc (cdr items)))))


Here is a perdy safe assumption of what Mapcar looks like. As you can see its a recursive procedure that terminates when it runs out of items to process. So I would almost concider Mapcar to be a loop in the word's "traditional" sense.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

daron

  • Guest
Looping techniques
« Reply #10 on: July 12, 2005, 01:20:05 PM »
Personally, I would use repeat when I don't feel the inclination to place a selection set in an itemized list. I've found foreach and mapcar to have similar uses with some differing results.

Tom Smith

  • Guest
Looping techniques
« Reply #11 on: July 12, 2005, 02:05:23 PM »
Per Se7en, I suppose we do have to consider mapcar a sort of a loop, in that it's defined recursively, and loops pretty much as foreach does.

Recursion itself is a related topic, as a way of structuring a routine. I'd think of it as a sort of glorified "while" process.

whdjr

  • Guest
Looping techniques
« Reply #12 on: July 12, 2005, 02:25:20 PM »
Quote from: Tom Smith
Per Se7en, I suppose we do have to consider mapcar a sort of a loop, in that it's defined recursively, and loops pretty much as foreach does.

Recursion itself is a related topic, as a way of structuring a routine. I'd think of it as a sort of glorified "while" process.

I don't want to argue but since we are having a discussion, I don't think the comparision to 'while' is accurate because 'while' will stop after it returns a 'nil' value whereas 'mapcar' iterates through the entire selection.

(just since we are discussing)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Looping techniques
« Reply #13 on: July 12, 2005, 02:41:14 PM »
So we can say that mapcar is like foreach
and vl-some is like while 8)
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.

Tom Smith

  • Guest
Looping techniques
« Reply #14 on: July 12, 2005, 03:30:45 PM »
Will, I guess I wasn't clear, I didn't intend to compare mapcar with while -- I said mapcar is pretty much like a foreach in operation in that it cycles over the whole list with no bail-out condition.

I was refering to recursion in general as being like a fancy while. The only relation to mapcar being that Se7en had showed how mapcar itself could be modelled as a recursion over a list.

A recursive function might depend on an if or a cond for a termination condition, and might often pass a list as an argument, but the only real defining feature is that the function calls itself, and keeps doing so until some terminating condition is met. In most cases you can rewrite a recursive function as an iterative one using while.

whdjr

  • Guest
Looping techniques
« Reply #15 on: July 12, 2005, 03:45:28 PM »
Ok, I got ya.

daron

  • Guest
Looping techniques
« Reply #16 on: July 12, 2005, 04:09:56 PM »
What about vl-every? Where does that fit in?