Hi everyone, I am getting my feet wet with core.async and am trying to attach atoms to channels that update automatically and print out to the console as you take and put. This is not for anything serious, just a way to get familiar with the behavior and functionality of the library and practice my Clojure skills. Thanks in advaan
Here is my code: (defn log-name [ch] (symbol (str ch '-log))) > > ;; Is this efficiently written? > (defmacro transparent-chan [ch] > (do > `(def ~ch (chan)) > `(def (log-name ~ch) (atom [])) > `(println "new transparent channel created!"))) > > (defn- update [log v] > (do > (swap! log conj v) > (println "log test passed: " v " has been logged") > (println "channel contents: " @log))) > > (defn transparent-put [ch v] > (let [log (log-name ch)] > (go > (let [ch ch > v v] > (>! ch v) > (println v " has been successfully moved through the channel"))) > (update log v) > (println "put test passed: " v " has been put on the channel") > (println " rechecking channel contents: " @log))) > > (defn transparent-take [ch] > (go > (let [v (<! ch) > log-name (symbol (str ch '-log))] > (swap! log-name #(remove #{v} %)) > (println v "has been removed from channel"))) > (println " removal pending")) > > ;; tests > (transparent-chan c) > c > c-log > @c-log > (transparent-put c 42) > (transparent-take c) My main questions are regarding variable scope: 1) I feel like it is a redundant to define bindings multiple times in my go's let exprs when the params have already been passed in as arguments. Is there a more idiomatic way to do this? 2) Whenever I try to create a generic process to automatically generate a log-name for each channel and then call update, I get a 'symbol cannot be cast to atom' error. How can I get around this? It seems to happen even if I don't use the log-name function. 3) Is there a better way to keep track of what's on a channel that doesn't use macros? -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.