4
我正在嘗試讀取大型文本文件並計算出現特定錯誤。 例如,對於下面的示例文本clojure讀取大型文本文件並對發生次數進行計數
something
bla
error123
foo
test
error123
line
junk
error55
more
stuff
我想結束了(真的不關心什麼數據結構,雖然我想到一個地圖)
error123 - 2
error55 - 1
這裏是我有什麼試過到目前爲止
(require '[clojure.java.io :as io])
(defn find-error [line]
(if (re-find #"error" line)
line))
(defn read-big-file [func, filename]
(with-open [rdr (io/reader filename)]
(doall (map func (line-seq rdr)))))
調用它像這樣
(read-big-file find-error "sample.txt")
回報:
(nil nil "error123" nil nil "error123" nil nil "error55" nil nil)
接下來,我試圖刪除零值和組類似物品
(group-by identity (remove #(= nil %) (read-big-file find-error "sample.txt")))
返回
{"error123" ["error123" "error123"], "error55" ["error55"]}
這是越來越接近期望的輸出,儘管它可能不高效。我現在怎樣才能得到這些數字?另外,作爲clojure和函數式編程的新手,我將不勝感激任何有關如何改進這一點的建議。 謝謝!
謝謝,這就是我一直在尋找的! –