rlm@10: ; Copyright (c) Christophe Grand, November 2008. All rights reserved. rlm@10: rlm@10: ; The use and distribution terms for this software are covered by the rlm@10: ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) rlm@10: ; which can be found in the file epl-v10.html at the root of this rlm@10: ; distribution. rlm@10: ; By using this software in any fashion, you are agreeing to be bound by rlm@10: ; the terms of this license. rlm@10: ; You must not remove this notice, or any other, from this software. rlm@10: rlm@10: ;; misc agent utilities rlm@10: rlm@10: ;; note to other contrib members: feel free to add to this lib rlm@10: rlm@10: (ns rlm@10: ^{:author "Christophe Grande", rlm@10: :doc "Miscellaneous agent utilities rlm@10: (note to other contrib members: feel free to add to this lib)", rlm@10: } rlm@10: clojure.contrib.agent-utils) rlm@10: rlm@10: (defmacro capture-and-send rlm@10: "Capture the current value of the specified vars and rebind rlm@10: them on the agent thread before executing the action. rlm@10: rlm@10: Example: rlm@10: (capture-and-send [*out*] a f b c)" rlm@10: rlm@10: [vars agent action & args] rlm@10: (let [locals (map #(gensym (name %)) vars)] rlm@10: `(let [~@(interleave locals vars) rlm@10: action# (fn [& args#] rlm@10: (binding [~@(interleave vars locals)] rlm@10: (apply ~action args#)))] rlm@10: (send ~agent action# ~@args))))