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

NMAP macro



(defmacro nmap (result-sequence function &rest sequences)
  (let (let-clauses min-args function-args
        gs
        (result-sequence-gs (gensym))
        (function-gs (gensym))
        (length (gensym))
        (rs (gensym))
        (result-element (gensym))
        (index (gensym)))
    (dolist (sequence (reverse sequences))
      (setq gs (gensym))
      (push '(,gs ,sequence) let-clauses)
      (push '(cond ((listp ,gs)
                    (pop ,gs))
                   (t
                    (aref ,gs ,index)))
            function-args)
      (push '(length ,gs) min-args))
    (push '(,function-gs ,function) let-clauses)
    (push '(,result-sequence-gs ,result-sequence) let-clauses)
    (push '(length ,result-sequence-gs) min-args)
    '(let (,@let-clauses
           (,length (length ,result-sequence-gs))
           (,rs ,result-sequence-gs)
           ,result-element)
       (setq ,length (funcall 'min ,@min-args))
       (dotimes (,index ,length)
         (setq ,result-element (funcall ,function-gs ,@function-args))
         (cond ((listp ,result-sequence-gs}i)
                (setf (car ,rs) ,result-element)
                (setq ,rs (cdr ,rs)))
               (t
                (setf (aref ,result-sequence-gs ,index) ,result-element))))
       ,result-sequence-gs)))

     For those who prefer a macro.  Don't know why I couldn't come up
with this last night.

Bob Sasseen
veracsd.rs@a.isi.edu (arpanet)