Hi all!

Here's a set of macros I have found useful for creating simulated
thread-local bindings for specific agents:

(defmacro bind-agent
  "Adds bindings to an agent's metadata"
  [#^clojure.lang.Agent a bindings]
  (list 'let (vector 'ba# a)
        (list 'do (list 'alter-meta!
                        'ba#
                        'assoc
                        :agent-bindings
                        (list 'quote bindings))
              'ba#)))

(defmacro send-bound
  "Send to an agent with metadata bindings"
  [#^clojure.lang.Agent a fun]
  (list 'send
        a
        (list 'fn ['& 'x#]
              (list 'binding (-> a qualify-sym find-var var-get
meta :agent-bindings)
                    (list 'apply fun 'x#)))))

Where qualify-sym is a private function in clojure.contrib.error-kit
which was referred to me by the helpful chaps in #clojure, and
attributed to Chouser.  Example:

user> (def z 1)
#'user/z
user> (def x (bind-agent (agent 1) [z 2]))
#'user/x
user> (+ @x z)
2
user> (send-bound x #(+ % z))
#<ag...@19c205b: 3>
user> @x
3


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to