lisp-random / once-only-cond.lisp

Robert Smith 01fc273 













Robert Smith 90035ec 
Robert Smith 01fc273 









Robert Smith 90035ec 

















Robert Smith 9e68477 
Robert Smith 90035ec 



Robert Smith 01fc273 
(defmacro with-cond-once (&body body)
  (let ((flags (gensym "FLAGS-")))
    `(let ((,flags (make-hash-table)))
       (macrolet ((cond-once (&body cond-forms)
                    `(cond
                       ,@(loop :for (pred . results) :in cond-forms
                               :for i :from 0
                               :collect
                               `(,pred
                                 (unless (gethash ,i ,',flags)
                                   (setf (gethash ,i ,',flags) t)
                                   ,@results))))))
         ,@body))))

(defun test-cond-once ()
  (with-cond-once
    (dotimes (i 10)
      (let ((r (random 3)))
        (format t "r=~D: " r)
        
        (cond-once
          ((= r 0) (princ "got 0"))
          ((= r 1) (princ "got 1"))
          ((= r 2) (princ "got 2")))
        
        (terpri)))))


(defmacro with-once (&body body)
  (let ((flags (gensym "FLAGS-")))
    `(let ((,flags (make-hash-table)))
       (macrolet ((once ()
                    (let ((g (gensym "ONCE-")))
                      `(unless (gethash ',g ,',flags)
                         (setf (gethash ',g ,',flags) t)))))
         ,@body))))

(defun test-once ()
  (with-once
    (dotimes (i 10)
      (let ((r (random 3)))
        (format t "r=~D: " r)
        
        (cond
          ((and (= r 0) (once)) (princ "got 0"))
          ((and (= r 1) (once)) (princ "got 1"))
          ((and (= r 2) (once)) (princ "got 2")))
        
        (terpri)))))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.