> 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?
I think the fact that you are re-binding in a let should show you that these variables are already in scope: that let is wholly redundant, just remove it. > 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. It's simple: a symbol is not an atom. It's telling you exactly what you need to know: you cannot call swap! on a symbol. Make sure your atom is the first argument to swap! wherever you use it. > 3) Is there a better way to keep track of what's on a channel that > doesn't use macros? You cannot look inside of a channel, but to do what you want and get a sense of how core.async works, I think you are on the right track by logging as you put stuff in and take it out. That said, I would start by getting rid of the log and the macro--these are unnecessary complications--and do things very simply: => (require '[clojure.core.async :as async]) nil => (def c (async/chan)) #'user/c => (def my-go-loop (async/go-loop [msg (async/<! c)] (println "got msg " msg " from channel " 'c))) #'user/my-go-loop => (defn put-and-print! [c msg] (println "putting msg " msg " onto channel " 'c) (async/put! c msg)) #'user/put-and-print! => (put-and-print! c "I am a message!") putting msg I am a message! onto channel c got msg I am a message! from channel c nil => Printing out "channel c" is kind of silly since there is only one, but it sounds like you may want to do this with multiple channels so this is one way to print out the variable name--simply quote it. DD (2014/05/12 11:41), gamma235 wrote: > 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 > <mailto:clojure+unsubscr...@googlegroups.com>. > For more options, visit https://groups.google.com/d/optout. -- 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.