本文介绍了如何读取stdin(* in *)在clojure中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写我的第一个clojure程序,并想从stdin读取行。



当我尝试这样:

 (doall(map#(println%)(line-seq * in *)))

我得到这个异常:

 线程mainjava.lang中的异常。 ClassCastException:clojure.lang.LineNumberingPushbackReader不能转换为java.io.BufferedReader(test.clj:0)

我在版本1.0和1.1中获得相同的结果



那么如何将 *在* seq我可以迭代?我会认为这是常见的, * in * 本身将是可迭代的,但这不工作 - 如果我尝试直接使用它: p>

  java.lang.IllegalArgumentException:不知道如何创建ISeq:clojure.lang.LineNumberingPushbackReader(NO_SOURCE_FILE:0)

另外,在clojure中有一些常规文件处理的例子吗?

解决方案

尝试包装 。并且使用 doseq 而不是 doall ,如devstopfix指出的:

 (doseq [ln(line-seq(java.io.BufferedReader。* in *))] 
(println ln))

请注意,记录为需要作为其来源。 / p>

I am writing my first clojure program, and want to read lines from stdin.

When I try this:

(doall (map #(println %) (line-seq *in*)))

I get this exception:

Exception in thread "main" java.lang.ClassCastException: clojure.lang.LineNumberingPushbackReader cannot be cast to java.io.BufferedReader (test.clj:0)

I get the same results in version 1.0 and 1.1

So how do I convert *in* into a seq I can iterate over? I would have thought that this is common enough that *in* itself would be iterable, but that does not work either - if I try to use it directly I get:

java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.LineNumberingPushbackReader (NO_SOURCE_FILE:0)

Also, are there any examples of doing general file handling in clojure?

解决方案

Try wrapping *in* in a java.io.BufferedReader. And also use doseq instead of doall, as devstopfix pointed out:

(doseq [ln (line-seq (java.io.BufferedReader. *in*))]
   (println ln))

Note that line-seq is documented to require a BufferedReader as its source.

这篇关于如何读取stdin(* in *)在clojure中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:14