Clojure(读取行)不等待输入

发布于 2024-12-09 02:59:37 字数 970 浏览 4 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花开浅夏 2024-12-16 02:59:37

尝试“lein蹦床跑”。请参阅 http://groups.google.com/group/leiningen/browse_thread/thread/ a07a7f10edb77c9b 还可以从 https://github.com/technomancy/leiningen

问:我无法访问项目内的标准输入。

答:Leiningen 用来生成新进程的库存在问题,会阻止对控制台输入的访问。这意味着像 read-line 这样的函数在大多数情况下都不会按预期工作,尽管 repl 任务必然包含解决方法。您还可以在 Leiningen 退出后使用 Trampoline 任务启动项目的 JVM,而不是将其作为子进程启动。

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:

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.

滥情稳全场 2024-12-16 02:59:37

我也遇到过类似的问题,并求助于构建一个 jar 文件,然后运行它。

lein uberjar
java -jar project-standalone.jar

虽然速度慢了一点,但还是让我摆脱了困境。从 repl 中有效的答案将是
变得更好

I have had similar problems and resorted to building a jar file and then running that.

lein uberjar
java -jar project-standalone.jar

It's a bit slower, though it got me unstuck. An answer that works from the repl would
be better

如果没结果 2024-12-16 02:59:37

使用宏 with-read-line-support 包装你的 read-line 调用,该宏现在位于 ns swank.core [自 swank-clojure 1.4+ 我相信]:

(use 'swank.core)
(with-read-line-support 
  (println "a line from Emacs:" (read-line)))

感谢 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]:

(use 'swank.core)
(with-read-line-support 
  (println "a line from Emacs:" (read-line)))

Thanks to Tavis Judd for the fix.

陌伤浅笑 2024-12-16 02:59:37

您可以使用read并使用字符串作为输入。

You can use read and use a string as input.

千秋岁 2024-12-16 02:59:37

不确定问题的主要方面,但在 emacs 中肯定不可能使 stdin 工作。但是,如果您想从用户那里获取文本,您可以使用 JOptionPane 轻松完成,就像我的小井字棋程序中的代码一样:

(defn get-input []
  (let [input (JOptionPane/showInputDialog "Enter your next move (row/column)")]
    (map #(Integer/valueOf %) (.split input "/"))))

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:

(defn get-input []
  (let [input (JOptionPane/showInputDialog "Enter your next move (row/column)")]
    (map #(Integer/valueOf %) (.split input "/"))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文