[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: loop macro



I think I prefer "lispy" iteration macros.  Here is an example of what I
mean by a lispy iteration macro:

;; Return a list of the items in list-of-items 
;; which pass the test TEST.
(iterate ((item in list-of-items))
  (when (test item)
    (collect item)))

I like this better because:

 -the bindings looks more like standard lisp bindings (let do etc).
 -I can use COLLECT, SUM etc. at any level in the body.
 -uses of collect sum etc. look like lisp function calls.
 -which makes it natural to use the existing when, unless etc.

NOTE: I am not proposing this version of ITERATE as something we
      should consider in and of itself, I am just using it to show
      some things I like about it.

Here are some more examples using it:

;; Basically FIRSTN
;; Get the first 10 items in a list
(iterate ((item in list)
          (i from 0 below 10))
  (collect y))


;; Sort of GETL.
;;
(iterate ((prop on plist by cddr))
  (when (memq (car prop) properties-to-collect)
    (collect prop)))

;; Return a left hand to match left-hand-to-match
;; or error if couldn't find one.
(iterate ((left in left-hands)
          (right in right-hands))
  (when (eq left left-to-match)
    (return right))
  (finally (error "Could find a right hand.")))