简体   繁体   English

clojure的读取文件结构,即with-open和clojure.java.io/reader,是否足够频繁访问?

[英]Is clojure's read file structure, i.e. with-open and clojure.java.io/reader, efficient enough for frequent access?

Suppose I write a function to parse data from a txt file with with-open and clojure.java.io/reader, and then I wrote another function to call the reader function multiple of times in order to process data, eg 假设我编写了一个函数来解析带有-ope和clojure.java.io/reader的txt文件中的数据然后我编写了另一个函数来多次调用reader函数来处理数据,例如

(defn grabDataFromFile [file patternString]   
     (let [data (atom [])]
        (with-open [rdr (clojure.java.io/reader file)] 
         (doseq [line (line-seq rdr)] 
           (if  (re-matches  (re-pattern  patternString) line) (swap! data conj line))))
        @data))


(defn myCalculation [file ]
  (let [data1 (grabDataFromFile file "pattern1")
        data2 (grabDataFromFile file "pattern2")
        data3 (grabDataFromFile file "pattern3")]
    ;calculations or processes of data1, data2, data3....))

My question is, inside this myCalculation function, is the underlying code smart enough to open the file just once with clojure reader, and get all data needed in one shot? 我的问题是,在myCalculation函数中,底层代码足够智能,只需用clojure reader打开文件一次,并获得一次性所需的所有数据? Or does it open and close the file as many times as number of calls for function grabDataFromFile ? 或者它是否打开和关闭文件的次数与函数grabDataFromFile的调用次数一样多? ( In this example, 3) (在这个例子中,3)

A follow up question would be, what can I do to speed up if the reader is not smart enough, and if I have to intentionally separate "parser" code with "processing" code? 一个后续问题是,如果读者不够聪明,我该怎么办才能加快速度,如果我不得不故意将“解析器”代码与“处理”代码分开?

grabDataFromFile will open and close reader (on exit) every time it is called. grabDataFromFile都会打开和关闭阅读器(退出时)。 The underlying code cannot be that smart such that a function can detect the context of its caller without some explicitly provided information. 底层代码不能那么聪明,以至于函数可以在没有明确提供信息的情况下检测其调用者的上下文。

Make grabDataFromFile to accept another function which is your parser logic that operates on each line (or it could be any function that you want to perform on each line) 使grabDataFromFile接受另一个函数,它是你在每一行上运行的解析器逻辑(或者它可以是你想在每一行上执行的任何函数)

(defn grabDataFromFile [file patternString process-fn]   
  (with-open [rdr (clojure.java.io/reader file)] 
    (doseq [line (line-seq rdr)] 
      (process-fn line))))




(defn myCalculation [file]
  (let [patterns [["pattern1" (atom [])]
                  ["pattern2" (atom [])]
                  ["pattern3" (atom [])]]
        pattern-fns (map (fn [[p data]]
                           (fn [line]
                             (if  (re-matches (re-pattern  p) line)                              
                               (swap! data conj line)))) patterns)
        pattern-fn (apply juxt pattern-fns)]
    (grabDataFromFile file pattern-fn)
    ;perform calc on patterns atoms
    ))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM