在Clojure中更加惯用的逐行文件处理

Joh*_*ker 3 clojure

我正在尝试使用Clojure 逐行读取(可能有也可能没有)YAML前置文件的文件,并返回带有两个向量的hashmap,一个包含frontmatter行,另一个包含其他所有内容(即正文) .

示例输入文件如下所示:

---
key1: value1
key2: value2
---

Body text paragraph 1

Body text paragraph 2

Body text paragraph 3
Run Code Online (Sandbox Code Playgroud)

我有这样的功能代码,但是对于我(对Clojure来说没有经验)鼻子,它充满了代码味道.

(defn process-file [f]
  (with-open [rdr (java.io.BufferedReader. (java.io.FileReader. f))]
    (loop [lines (line-seq rdr) in-fm 0 frontmatter [] body []]
      (if-not (empty? lines)
        (let [line (string/trim (first lines))]
          (cond
            (zero? (count line))
              (recur (rest lines) in-fm frontmatter body)
            (and (< in-fm 2) (= line "---")) 
              (recur (rest lines) (inc in-fm) frontmatter body)
            (= in-fm 1)  
              (recur (rest lines) in-fm (conj frontmatter line) body)
            :else          
             (recur (rest lines) in-fm frontmatter (conj body line))))
        (hash-map :frontmatter frontmatter :body body)))))
Run Code Online (Sandbox Code Playgroud)

有人能指出我更优雅的方式吗?我将在这个项目中进行大量的逐行解析,如果可能的话,我想要一个更惯用的方法来解决它.

Mic*_*zyk 6

首先,我将线处理逻辑放在它自己的函数中,从实际读取文件的函数中调用.更好的是,你可以使处理IO的函数采用函数来映射作为参数的行,也许沿着这些方向:

(require '[clojure.java.io :as io])

(defn process-file-with [f filename]
  (with-open [rdr (io/reader (io/file filename))]
    (f (line-seq rdr))))
Run Code Online (Sandbox Code Playgroud)

请注意,这种安排使得有责任f在返回之前实现尽可能多的行seq(因为之后with-open将关闭行seq的底层读取器).

鉴于这种责任划分,行处理函数可能看起来像这样,假设第一行---必须是第一个非空行,并且要跳过所有空白行(就像使用问题文本中的代码时一样):

(require '[clojure.string :as string])

(defn process-lines [lines]
  (let [ls (->> lines
                (map string/trim)
                (remove string/blank?))]
    (if (= (first ls) "---")
      (let [[front sep-and-body] (split-with #(not= "---" %) (next ls))]
        {:front (vec front) :body (vec (next sep-and-body))})
      {:body (vec ls)})))
Run Code Online (Sandbox Code Playgroud)

请注意,调用会vec导致所有行被读入并在向量或向量对中返回(这样我们就可以process-linesprocess-file-with没有读取器过早关闭的情况下使用).

因为磁盘上实际文件的读取行现在与处理seq行分离,所以我们可以在REPL上轻松测试进程的后半部分(当然这可以做成单元测试):

;; could input this as a single string and split, of course
(def test-lines
  ["---"
   "key1: value1"
   "key2: value2"
   "---"
   ""
   "Body text paragraph 1"
   ""
   "Body text paragraph 2"
   ""
   "Body text paragraph 3"])
Run Code Online (Sandbox Code Playgroud)

现在调用我们的功能:

user> (process-lines test-lines)
{:front ("key1: value1" "key2: value2"),
 :body ("Body text paragraph 1"
        "Body text paragraph 2"
        "Body text paragraph 3")}
Run Code Online (Sandbox Code Playgroud)