0

我正在学习 Clojure 并研究一个简单的文件解析脚本。

我有一个格式为:

pattern1
pattern2
pattern3
pattern1
pattern2
...

每行都有一些我提取的值(数字)。

例如,如果我用 Java 编写这个,我会做类似的事情:

Map<String, Integer> parse(String line) {
    //using Optional in this toy example, but could be an empty map or something else to
    //signal if first one was actually matched and the values are there
    Optional<Map<String, Integer>> firstMatched = matchFirst(line);
    if (firstMatched.isPresent()) {
        return firstMatched.get();
    }
    //...do the same for 2 remaining patterns
    //...
}

现在,在 Clojure中做类似事情的优雅或惯用方式是什么?

我想我可以使用cond,但由于测试表达式中没有绑定,我将不得不两次解析该行:

(defn parse
  [line]
  (cond
    (re-find #"pattern-1-regex" line) (re-find...)
    (re-find #"pattern-2-regex" line) (re-find...

我也可以使用if-let,但这将是很多嵌套,因为有 3 个不同的选项。想象一下 7 种不同的模式会是什么样子。

有什么建议么?显然,Java 解决方案是一种必要的解决方案,我可以随时“返回”,那么处理这个简单分支的Clojure/FP 方式是什么。

4

2 回答 2

1

我会用一些简单的函数来返回第一个匹配的模式,过滤模式序列:

(defn first-match [patterns]
  (fn [line]
    (some #(re-find % line) patterns)))

这个返回函数,它将返回第一个匹配,测试行:

user> (def mat (first-match [#"(asd)" #"(fgh)" #"aaa(.+?)aaa"]))
#'user/mat

user> (mat "aaaxxxaaa")
;;=> ["aaaxxxaaa" "xxx"]

user> (mat "nomatch")
;;=> nil

否则你可以使用一些简单的宏。也许是这样的:

(defmacro when-cond [& conds]
  (when (seq conds)
    `(if-let [x# ~(first conds)]
       x#
       (when-cond ~@(rest conds)))))

user> 
(let [line "somethingaaa"]
  (when-cond
    (re-find #"something" line)
    (re-find #"abc(.*?)def" line)))
;;=> "something"

对于前面的示例,它将扩展到这样的内容(示意性地)

(if-let [x__8043__auto__ (re-find #"something" line)]
  x__8043__auto__
  (if-let [x__8044__auto__ (re-find #"abc(.*?)def" line)]
    x__8044__auto__
    nil))

更多示例:

user> 
(let [line "nomatch"]
  (when-cond
    (re-find #"something" line)
    (re-find #"abc(.*?)def" line)))
;;=> nil

user> 
(let [line "abcxxxxxxdef"]
  (when-cond
    (re-find #"something" line)
    (re-find #"abc(.*?)def" line)))
;;=> ["abcxxxxxxdef" "xxxxxx"]
于 2020-04-26T15:26:20.227 回答
1

给定一些样本数据:

(ns tst.demo.core
  (:use demo.core tupelo.core tupelo.test)
  (:require
    [clojure.string :as str]
    [tupelo.string :as ts]
    [tupelo.parse :as parse]))

(def data-str "
  fred123 1 2 3
  fred456   4 5    6
  wilma12  1.2
  wilma34 3.4
  barney1 1
  barney2 2
  ")

然后,您可以为每种类型的数据定义解析函数:

(defn fred-parser
  [line]
  (let [tokens        (str/split line #"\p{Blank}+")
        root          (first tokens)
        details       (rest tokens)
        parsed-root   (re-find #"fred\n*" root)
        parsed-params (mapv parse/parse-long details)
        result        {:root parsed-root :params parsed-params}]
    result))

(defn wilma-parser
  [line]
  (let [tokens        (str/split line #"\p{Blank}+")
        root          (first tokens)
        details       (rest tokens)
        parsed-root   (re-find #"wilma\n*" root)
        parsed-params (mapv parse/parse-double details)
        result        {:root parsed-root :params parsed-params}]
    result))

我会制作一个从模式到解析函数的映射:

(def pattern->parser
  {#"fred\d*"  fred-parser
   #"wilma\d*" wilma-parser
   })

以及为每一行(已清理)数据找到正确解析器的一些函数:

(defn parse-line
  [line]
  (let [patterns          (keys pattern->parser)
        patterns-matching (filterv ; keep pattern if matches
                            (fn [pat]
                              (ts/contains-match? line pat))
                            patterns)
        num-matches       (count patterns-matching)]
    (cond
      (< 1 num-matches) (throw (ex-info "Too many matching patterns!" {:line line :num-matches num-matches}))
      (zero? num-matches) (prn :no-match-found line)
      :else (let [parser      (get pattern->parser (only patterns-matching))
                  parsed-line (parser line)]
              parsed-line))))

(defn parse-file
  [data]
  (let
    [lines       (filterv #(not (str/blank? %)) ; remove blank lines
                   (mapv str/trim ; remove leading/trailing whitespace
                     (str/split-lines data))) ; split into lines
     parsed-data (mapv parse-line lines)]
    parsed-data))

和一个单元测试来展示它的实际效果:

(dotest
  (is= (parse-file data-str)
    [{:root "fred", :params [1 2 3]}
     {:root "fred", :params [4 5 6]}
     {:root "wilma", :params [1.2]}
     {:root "wilma", :params [3.4]}
     nil
     nil])
  )

请注意,不匹配的行返回 nil。您可能希望针对问题抛出异常,或者至少过滤掉这些nil值。现在你只得到一个打印的错误消息:

-------------------------------
   Clojure 1.10.1    Java 14
-------------------------------

Testing tst.demo.core
:no-match-found "barney1 1"
:no-match-found "barney2 2"

Ran 2 tests containing 1 assertions.
0 failures, 0 errors.

更多文档在这里这里

于 2020-04-26T17:53:08.477 回答