Clojure - 编写 split-first 和 split-last 的惯用方式

Clojure - idiomatic way to write split-first and split-last

编写以下函数的惯用方式是什么?

(split-first #"." "abc.def.ghi") ;;=>["abc", "def.ghi"]
(split-last #"." "abc.def.ghi") ;;=>["abc.def", "ghi"]

使用 split 有一个明显的(丑陋的?)解决方案,但我确定还有更优雅的解决方案?也许使用 regexes/indexOf/split-with ?

对于先拆分,最好的方法是:

(defn split-first [re s]
  (clojure.string/split s re 2))

(split-first #"\." "abc.def.ghi") 
=> ["abc" "def.ghi"]

(split-first #"<>" "abc<>def<>ghi")
=> ["abc" "def<>ghi"]

split-last 的一种方法是使用否定先行断言:

(defn split-last [re s]
  (let [pattern (re-pattern (str re "(?!.*" re ")"))]
    (split-first pattern s)))

(split-last #"\." "abc.def.ghi")
=> ["abc.def" "ghi"]

(split-last #"<>" "abc<>def<>ghi")
=> ["abc<>def" "ghi"]

如果您不一定需要正则表达式匹配,这会起作用:

(defn index-of
  [^String s c]
  (.indexOf s c))

(defn last-index-of
  [^String s c]
  (.lastIndexOf s c))

(defn split-at-index-fn
  [f c s]
  (let [i (f s c)]
    ; I do not know what you want returned if no match is found
    (when-not (neg? i)
      [(.substring s 0 i) (.substring s (inc i))])))

(def split-first
  (partial split-at-index-fn index-of))

(def split-last
  (partial split-at-index-fn last-index-of))

这不是很地道,因为它主要只是 Java 互操作。