当长度为 4 时,以下循环执行 5 次。从流中读取 5 个字符。
(loop [i (.read stream) result "" counter length]
(let [c (char i)]
(println "=>" c)
(if (zero? counter)
result
(recur (.read stream) (str result c) (dec counter)))))
你应该zero?
在做之前测试一下read
。请注意,read
即使length
== 0 开始,您的版本也会调用一次。
(loop [result "" counter length]
(if (zero? counter)
result
(let [c (char (.read stream))]
(println "=>" c )
(recur (str result c) (dec counter)))))
另一种避免显式的方法loop
:
(apply str
(take length
(repeatedly #(let [c (char (.read stream))]
(println "=>" c) c)))))
我不知道 clojure,但在我看来,您正在以“结果”形式再次阅读流,这像 CL 中的 final 吗?