小编Dav*_* J.的帖子

懒惰地从大文件中提取线条

我试图通过Clojure的大(> 1GB)文件中的行号抓取5行.我快到了,但看到了一些奇怪的事情,我想知道发生了什么.

到目前为止我有:

(defn multi-nth [values indices]
  (map (partial nth values) indices))

(defn read-lines [file indices]
  (with-open [rdr (clojure.java.io/reader file)]
    (let [lines (line-seq rdr)]
      (multi-nth lines indices))))
Run Code Online (Sandbox Code Playgroud)

现在,(read-lines "my-file" [0])工作没有问题.但是,传入[0 1]给我以下堆栈跟踪:

java.lang.RuntimeException: java.io.IOException: Stream closed
        Util.java:165 clojure.lang.Util.runtimeException
      LazySeq.java:51 clojure.lang.LazySeq.sval
      LazySeq.java:60 clojure.lang.LazySeq.seq
         Cons.java:39 clojure.lang.Cons.next
          RT.java:769 clojure.lang.RT.nthFrom
          RT.java:742 clojure.lang.RT.nth
         core.clj:832 clojure.core/nth
         AFn.java:163 clojure.lang.AFn.applyToHelper
         AFn.java:151 clojure.lang.AFn.applyTo
         core.clj:602 clojure.core/apply
        core.clj:2341 clojure.core/partial[fn]
      RestFn.java:408 clojure.lang.RestFn.invoke
        core.clj:2430 clojure.core/map[fn]
Run Code Online (Sandbox Code Playgroud)

在我从文件中读取第二行之前,似乎关闭了流.有趣的是,如果我手动从文件中提取一行,那么(nth lines 200)multi-nth调用适用于所有值<= 200.

知道发生了什么事吗?

clojure stream-processing lazy-evaluation

7
推荐指数
2
解决办法
2690
查看次数