为什么clojure的map println只在repl中起作用? [英] why does clojure's map println only works in repl?

查看:70
本文介绍了为什么clojure的map println只在repl中起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 lein新应用test-println 创建一个clojure应用并使用 lein repl 启动repl,然后输入(地图println [1 2 3 4 5 6])并得到预期的结果:

test- println.core => (地图println [1 2 3 4 5 6])
1
2
3
4
5
6
(nil nil nil nil nil nil)

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]))

精益生产仅打印您好,世界!

推荐答案

地图很懒。引用文档的第一句(加重):

map is lazy. To quote the first sentence of the documentation (emphasis added):


返回由结果组成的惰性序列将f应用于每个coll的
一组第一项,然后将f应用于每个coll的
一组第二项,直到任何一个colls都用完。

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 可以解决此问题,但是您可能应该查看

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆