Are there any differences between doseq and for combined with dorun?
我很清楚
1 2 | (defmacro doseq' [bindings & body] `(dorun (for ~bindings ~@body))) |
此实现与Clojure的
从实际的角度来看,
1 | user> (require '[clojure.core.async :refer [chan go <! <!! >!]]) |
使用剂量q:
1 2 3 4 5 6 7 8 9 10 | user> (let [stuff (chan)] (go (while true (println (<! stuff)))) (go (doseq [a (range 4)] (>! stuff a)))) #object[clojure.core.async.impl.channels.ManyToManyChannel 0x523a18bc"clojure.core.async.impl.channels.ManyToManyChannel@523a18bc"] user> 0 1 2 3 |
使用for和dorun:
1 2 3 4 5 6 | user> (let [stuff (chan)] (go (while true (println (<! stuff)))) (go (dorun (for [a (range 4)] (>! stuff a))))) CompilerException java.lang.IllegalArgumentException: No method in multimethod '-item-to-ssa' for dispatch value: :fn, compiling:(form-init5662188991458325584.clj:4:9) |
失败,因为它试图越过go块内的函数调用,因此脱离了go宏的作用域。