为什么 clojure 的 map println 只适用于 repl? [英] why does clojure's map println only works in repl?
问题描述
我使用 lein new app test-println
创建一个 clojure 应用程序并使用 lein repl
启动 repl,然后我输入 (map println [1 23 4 5 6])
并得到预期的结果:<代码>test-println.core=>(映射 println [1 2 3 4 5 6])123456(零零零零零零零零)
I use lein new app test-println
to create a clojure app and launch the repl with lein repl
, then I enter (map println [1 2 3 4 5 6])
and get the expected result:
test-println.core=> (map println [1 2 3 4 5 6])
1
2
3
4
5
6
(nil nil nil nil nil nil)
但是,如果我将 (map println [1 2 3 4 5 6])
添加到 src/test_println/core.clj
的末尾:
However if I add (map println [1 2 3 4 5 6])
to the end of src/test_println/core.clj
:
(ns test-println.core
(:gen-class))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "Hello, World!")
(map println [1 2 3 4 5 6]))
lean run
只打印 Hello, World!
.
推荐答案
map 是懒惰的.引用文档的第一句话(强调):
map is lazy. To quote the first sentence of the documentation (emphasis added):
返回一个 lazy 序列,其中包含将 f 应用于每个 coll 的第一项的集合,然后将 f 应用于集合每个 coll 中的第二个项目,直到任何一个 coll 用完.
Returns a lazy sequence consisting of the result of applying f to the set of first items of each coll, followed by applying f to the set of second items in each coll, until any one of the colls is exhausted.
REPL 强制对表达式求值以显示结果,但您的代码中没有任何内容.dorun 可以解决这个问题,但您可能应该查看 doseq/doall.
The REPL forces evaluation of the expression to show the result, but nothing in your code does. dorun would solve this, but you probably should look at doseq / doall instead.
这篇关于为什么 clojure 的 map println 只适用于 repl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!