gpt4 book ai didi

clojure - 在 Clojure 中使用 java.io/reader 时获得副作用的正确方法?

转载 作者:行者123 更新时间:2023-12-02 21:28:16 25 4
gpt4 key购买 nike

我正在从一个非常大的文本文件中读取行。该文件包含一组数据,我想从中选择特定的行号。我想做的是从文件中读取一行,如果该行是我想要的,则将其与我的结果相结合,如果不是,则检查下一行。我不想将我在内存中看到的所有行存储起来,因此我希望有一种方法可以在读取它们时将它们从阅读器行序列中删除。

我有一个这样的函数:

;; evaluates but doesn't modify the line sequence so continuously adds 
;; the same first line to the result. I would like this exact function
;; but somehow have it drop the first line of lines at each iteration.
(defn get-training-data [batch-size batch-num]
(let [line-numbers (fn that returns vector of random numbers)]
(with-open [rdr (clojure.java.io/reader "resources/sample.txt")]
(let [lines (line-seq rdr) res []]
(for [i (range (apply max line-numbers))
:let [res (conj res (json/read-str (first lines)))]
:when (some #{i} line-numbers)]
res)))))

我也有这样的功能:

;;this works as I want it to, but only with a small file and produces a 
;;stack overflow with a large file
(defn get-training-data1 [batch-size batch-num]
(let [line-numbers (fn that returns a vector of random numbers)]
(with-open [rdr (clojure.java.io/reader "resources/sample.txt")]
(let [lines (line-seq rdr)]
(loop [i 0 f (apply max line-numbers) res [] lines lines]
(if (> i f)
res
(if (some #{i} line-numbers)
(recur
(inc i)
f
(conj res (json/read-str (first lines)))
(drop 1 lines))
(recur
(inc i)
f
res
(drop 1 lines)))))))))

当我尝试对此进行测试时,我开发了以下更简单的案例:

;;works
(let [res []]
(for [i (range 10)
:let [res (conj res i)]
:when (odd? i)]
res)) ;;([1] [3] [5] [7] [9])

;;now an attempt to get the same result but have a side effect each time,
;;produces null pointer exception.
(let [res []]
(for [i (range 10)
:let [res (conj res i)]
:when (odd? i)]
(doall
(println i)
res)))

我相信,如果我能弄清楚如何在 for 中产生副作用,那么第一个问题就会得到解决,因为我可以使副作用删除读者行序列的第一行。

大家有什么想法吗?

最佳答案

映射和过滤器可以很好地做到这一点并保持惰性,这样您就不会在内存中存储超出需要的内容。

user> (->> (line-seq (clojure.java.io/reader "project.clj")) ;; lazy sequence of lines
(map vector (range)) ;; add an index
(filter #(#{1 3 7 9} (first %))) ;; filter by index
(map second )) ;; drop the index

(" :description \"API server for Yummly mobile app(s)\""
"[com.project/example \"1.4.8-SNAPSHOT\"]"
" [org.clojure/tools.cli \"0.2\.4\"]"
" [clojurewerkz/mailer \"1.0.0-alpha3\"]")

关于clojure - 在 Clojure 中使用 java.io/reader 时获得副作用的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22973618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com