Clojure(读取行)不等待输入
我正在用 Clojure 编写一个文本游戏。我希望玩家在控制台上输入行,然后游戏逐行响应。
研究表明 (read-line)
是一种从 Clojure 中的标准输入获取文本行的方式,但它对我不起作用。
我在一个新的 Leiningen 项目中,并且在 project.clj
中添加了一个 :main
子句,指向唯一的源文件:
(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
code> 产生:
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
运行。
I am writing a text game in Clojure. I want the player to type lines at the console, and the game to respond on a line-by-line basis.
Research showed me that (read-line)
is the way one is meant to get text lines from standard input in Clojure, but it is not working for me.
I am in a fresh Leiningen project, and I have added a :main
clause to the project.clj
pointing to the only source file:
(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"))
using lein run
yields:
Entering -main
ECHO: nil
Exiting -main
In other words, there is no opportunity to enter text at the console for (read-line)
to read.
How should I get Clojure to wait for characters and newline to be entered and return the corresponding string?
(I am using GNOME Terminal 2.32.1 on Linux Mint 11, Leiningen 1.6.1.1 on Java 1.6.0_26 Java HotSpot(TM) 64-Bit Server VM, Clojure version 1.2.1.)
Update: If I run lein repl
, I can (println (read-line))
, but not when I have a -main
function and run using lein run
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试“lein蹦床跑”。请参阅 http://groups.google.com/group/leiningen/browse_thread/thread/ a07a7f10edb77c9b 还可以从 https://github.com/technomancy/leiningen:
Try "lein trampoline run". See http://groups.google.com/group/leiningen/browse_thread/thread/a07a7f10edb77c9b for more details also from https://github.com/technomancy/leiningen:
我也遇到过类似的问题,并求助于构建一个 jar 文件,然后运行它。
虽然速度慢了一点,但还是让我摆脱了困境。从 repl 中有效的答案将是
变得更好
I have had similar problems and resorted to building a jar file and then running that.
It's a bit slower, though it got me unstuck. An answer that works from the repl would
be better
使用宏 with-read-line-support 包装你的 read-line 调用,该宏现在位于 ns swank.core [自 swank-clojure 1.4+ 我相信]:
感谢 Tavis Judd 的修复。
Wrap your read-line calls with the macro with-read-line-support which is now in ns swank.core [since swank-clojure 1.4+ I believe]:
Thanks to Tavis Judd for the fix.
您可以使用
read
并使用字符串作为输入。You can use
read
and use a string as input.不确定问题的主要方面,但在 emacs 中肯定不可能使 stdin 工作。但是,如果您想从用户那里获取文本,您可以使用 JOptionPane 轻松完成,就像我的小井字棋程序中的代码一样:
Not sure about the lein aspects of the problem, but definitely in emacs it is impossible to make stdin work. However, if you want to get text from the user, you can easily do it using a JOptionPane like this code from my little tic-tac-toe program: