为什么clojure中的map返回异步chan而不是字符串序列?

时间:2017-10-03 14:15:33

标签: clojure

我是clojure的新手,目前仍然坚持使用map函数。我尝试了以下代码:

(map #(str "Hello " % "!" ) ["Ford" "Arthur" "Tricia"])

它回来了:

#object[clojure.core.async.impl.channels.ManyToManyChannel 0x2eaf8b7c "clojure.core.async.impl.channels.ManyToManyChannel@2eaf8b7c"]

虽然我想:

("Hello Ford!" "Hello Arthur!" "Hello Tricia!"])

我跟着 the clojure.core/map documentation example但它没有按预期工作..

1 个答案:

答案 0 :(得分:3)

看起来您可能已将clojure.core/map替换为clojure.core.async/map。这种情况最常见的方式是在你的repl上做一些这样的事情(请注意当你这样做的时候会有很多警告告诉你):

user=> (use 'clojure.core.async)
WARNING: reduce already refers to: #'clojure.core/reduce in namespace: user, being replaced by: #'clojure.core.async/reduce
WARNING: take already refers to: #'clojure.core/take in namespace: user, being replaced by: #'clojure.core.async/take
WARNING: map already refers to: #'clojure.core/map in namespace: user, being replaced by: #'clojure.core.async/map
WARNING: transduce already refers to: #'clojure.core/transduce in namespace: user, being replaced by: #'clojure.core.async/transduce
WARNING: into already refers to: #'clojure.core/into in namespace: user, being replaced by: #'clojure.core.async/into
WARNING: partition already refers to: #'clojure.core/partition in namespace: user, being replaced by: #'clojure.core.async/partition
WARNING: merge already refers to: #'clojure.core/merge in namespace: user, being replaced by: #'clojure.core.async/merge
WARNING: partition-by already refers to: #'clojure.core/partition-by in namespace: user, being replaced by: #'clojure.core.async/partition-by

您可以随时使用它的限定名称明确引用某个功能:

(clojure.core/map #(str "Hello " % "!" ) ["Ford" "Arthur" "Tricia"])

通常情况下,尽管最好避免使用use而不是require另一个命名空间,而是给它一个别名:

(require '[clojure.core.async :as async])

这样,您的clojure.core/map仍然可以用作map(因为所有clojure.core函数都是自动引用的),而异步版本可以称为async/map。< / p>