Clojure(read-line)返回nil;不提示 [英] Clojure's (read-line) returns nil; does not prompt

查看:184
本文介绍了Clojure(read-line)返回nil;不提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的第一个正确的Clojure程序 - 一个象棋游戏。我有以下:

 (defn human-move [board] 
(board-utils / print-board )
(printEnter your move,like this:'E7 E5'...)
(loop [raw-move(terminal-input)] ;;(read-line) $ b(printlnI just receivedraw-move)
(if(re-matches#[AH] [1-8] \s [AH] [1-8]raw-move)
(parse-move raw-move)
(do
(println格式不正确,应该有字母,数字,空格,字母和最后数字)
打印再试一次:)
(recur(read-line))))))

请注意 read-line 被注释掉并替换为 terminal-input 的位置。 read-line 给我一个NullPointerException,因此用于诊断目的:

  
(printnnreceivedwhatnot))
(printnninput)
(printnninput

然后,当我调用 human-move

  ... 
+ --- + --- + --- + --- + - + --- + --- + --- +
| P | P | P | P | P | P | P | P |
+ --- + --- + --- + --- + --- + --- + --- + --- +
| R | N | B | Q | K | B | N | R |
+ --- + --- + --- + --- + --- + --- + --- + --- +
输入您的移动,如下所示:'E7 E5'...输入
收到nil
我刚收到nil

从来没有机会输入内容作为输入。这是Java,我会开始玩垃圾收集器的小游戏(调用 Scanner.next(),例如),但与Clojure我不知道什么除了将(flush)放入其中。



对于值得的,这是与SLIME。 p>




我也尝试了 terminal-input 提供虚拟数据,得知我显然使用 loop / recur 不正确。我没有彻底调查过,因为我已经分心的 read-line 问题。



b
$ b

提前感谢。

解决方案

这将与swank-clojure 1.4.0 -SNAPSHOT如果你在swank.core / with-read-line-support中调用read-line,就像这样

  with-read-line-support(println一行来自Emacs:(read-line))

https://github.com/technomancy/swank-clojure/commit/f4a1eebc4d34f2ff473c4e5350f889ec356f5168


I'm working on my first proper Clojure program--a chess game. I have the following:

(defn human-move [board]
  (board-utils/print-board board)
  (print "Enter your move, like this: 'E7 E5' ...")
  (loop [raw-move (terminal-input)] ;;(read-line)]
    (println "I just received" raw-move)
    (if (re-matches #"[A-H][1-8]\s[A-H][1-8]" raw-move)
      (parse-move raw-move)
      (do
        (println "Invalid format! There should be a letter, number, space, letter, and final number.")
        (print "Try again: ")
        (recur (read-line))))))

Note the place where read-line is commented out and replaced by terminal-input. read-line was giving me a NullPointerException, so for diagnostic purposes:

(defn terminal-input []
  (println "input")
  (let [whatnot (read-line)]
    (println "received" whatnot)
    whatnot))

Then, when I call human-move.

...
+---+---+---+---+---+---+---+---+
| P | P | P | P | P | P | P | P |
+---+---+---+---+---+---+---+---+
| R | N | B | Q | K | B | N | R |
+---+---+---+---+---+---+---+---+
Enter your move, like this: 'E7 E5' ...input
received nil
I just received nil

I never got a chance to type something in as input. Were this Java, I'd start playing little games with the garbage collector (calling Scanner.next(), for instance), but with Clojure I didn't know what to do besides putting the (flush) in there.

For what it's worth, this is with SLIME.


I also tried to have terminal-input provide dummy data, and learned that I'm apparently using loop/recur incorrectly. I haven't investigated it super-thoroughly though, since I've been distracted by the read-line issues.

Thanks in advance.

解决方案

This will work now with swank-clojure 1.4.0-SNAPSHOT if you wrap the call to read-line in swank.core/with-read-line-support like this

(with-read-line-support (println "a line from Emacs:" (read-line))

https://github.com/technomancy/swank-clojure/commit/f4a1eebc4d34f2ff473c4e5350f889ec356f5168

这篇关于Clojure(read-line)返回nil;不提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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