Clojure(读行)不等待输入

标签 clojure console-application user-input leiningen

我正在 Clojure 中编写文本游戏。我希望玩家在控制台上输入行,而游戏则逐行响应。

研究表明 (read-line)是一种从 Clojure 中的标准输入获取文本行的方式,但它对我不起作用。

我在一个新的 Leiningen 项目中,我添加了一个 :main条款给 project.clj指向唯一的源文件:

(ns textgame.core)

(defn -main [& args]
  (println "Entering -main")
;  (flush)                      ;makes no difference if flush are commented out
  (let [input (read-line)]
    (println "ECHO:" input))
;  (flush)
  (println "Exiting -main"))

使用 lein run产量:
Entering -main
ECHO: nil
Exiting -main

换句话说,没有机会在控制台输入 (read-line) 的文本。阅读。

我应该如何让 Clojure 等待输入字符和换行符并返回相应的字符串?

(我在 Linux Mint 11 上使用 GNOME Terminal 2.32.1,在 Java 1.6.0_26 Java HotSpot(TM) 64 位服务器 VM 上使用 Leiningen 1.6.1.1,Clojure 版本 1.2.1。)

更新:如果我运行 lein repl ,我可以(println (read-line)) ,但不是当我有 -main函数并使用 lein run 运行.

最佳答案

试试“lein 蹦床跑”。见 http://groups.google.com/group/leiningen/browse_thread/thread/a07a7f10edb77c9b更多详情也来自 https://github.com/technomancy/leiningen :

Q: I don't have access to stdin inside my project.

A: There's a problem in the library that Leiningen uses to spawn new processes that blocks access to console input. This means that functions like read-line will not work as expected in most contexts, though the repl task necessarily includes a workaround. You can also use the trampoline task to launch your project's JVM after Leiningen's has exited rather than launching it as a subprocess.

关于Clojure(读行)不等待输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7707558/

相关文章:

scala - Clojure clojure.core.reducers/fold 和 Scala Fold 有什么区别?

vector - clojure - 从引用向量中删除一个元素

clojure - Clojure 中的高阶 if-then-else?

functional-programming - Clojure 中的运算符重载

c# - 由任务计划程序启动时隐藏 C# 控制台应用程序窗口

c - 获取用户输入并在需要时停止循环 C

c# - 将控制台光标设置为粗/细

c# - 围绕光标绘制一个圆圈 (C#)

c - 在 C 循环中将节点插入 BST

c++ - C++错误处理