Clojure Agent question - using send-off -


i have couple of questions following code:

(import   '(java.awt color graphics dimension)  '(java.awt.image bufferedimage)  '(javax.swing jpanel jframe))  (def width 900) (def height 600)   (defn render  [g]  (let [img (new bufferedimage width height                   (. bufferedimage type_int_argb))        bg (. img (getgraphics))]    (doto bg       (.setcolor (. color white))       (.fillrect 0 0 (. img (getwidth)) (. img (getheight)))       (.setcolor (. color red))       (.drawoval 200 200 (rand-int 100) (rand-int 50)))    (. g (drawimage img 0 0 nil))    (. bg (dispose))    ))  (def panel (doto (proxy [jpanel] []                         (paint [g] (render g)))              (.setpreferredsize (new dimension                                       width                                       height))))  (def frame (doto (new jframe) (.add panel) .pack .show))  (def animator (agent nil))   (defn animation    [x]   (send-off *agent* #'animation)   (. panel (repaint))   (. thread (sleep 100)))  (send-off animator animation) 
  1. in animation function - why #' used before animation in send-off?
  2. why send-off @ start of animation function work? shouldn't go start of animation function again , never execute repaint , sleep methods?
  3. is there disadvantage, compared original, in writing animation function as:

    (defn animation    [x]   (. panel (repaint))   (. thread (sleep 100))   (send-off *agent* animation)) 

in animation function - why #' used before animation in send-off?

to demonstrate clojure's dynamic nature.

the form #'animation var, 1 of clojure's mutable reference types. defn macro creates var. convenience, invoking var refers function same invoking function itself. var, unlike function, can change! redefine #'animation @ clojure repl , see effects.

using (send-off *agent* #'animation) forces clojure of current value of #'animation var every time. if code had used (send-off *agent* animation) instead, clojure value once, , not possible change animation function without stopping loop.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -