替换子序列的功能

时间:2014-09-05 16:48:59

标签: list vector replace clojure sequence

是否有可以替换子序列的函数?例如:

user> (good-fnc [1 2 3 4 5] [1 2] [3 4 5])
;; => [3 4 5 3 4 5]

我知道字符串有clojure.string/replace

user> (clojure.string/replace "fat cat caught a rat" "a" "AA")
;; => "fAAt cAAt cAAught AA rAAt"

矢量和列表有类似的东西吗?

3 个答案:

答案 0 :(得分:2)

这对你有用吗?

(defn good-fnc [s sub r]
  (loop [acc []
         s s]
    (cond
      (empty? s) (seq acc)
      (= (take (count sub) s) sub) (recur (apply conj acc r)
                                          (drop (count sub) s))
      :else (recur (conj acc (first s)) (rest s)))))

答案 1 :(得分:1)

这是一个与懒惰的seq输入很好地兼容的版本。请注意,它可以采用无限延迟序列(range),而不会像基于循环的版本那样无限循环。

(defn sq-replace
  [match replacement sq]
  (let [matching (count match)]
    ((fn replace-in-sequence [[elt & elts :as sq]]
       (lazy-seq
        (cond (empty? sq)
              ()
              (= match (take matching sq))
              (concat replacement (replace-in-sequence (drop matching sq)))
              :default
              (cons elt (replace-in-sequence elts)))))
     sq)))

#'user/sq-replace
user> (take 10 (sq-replace [3 4 5] ["hello, world"] (range)))
(0 1 2 "hello, world" 6 7 8 9 10 11)

我冒昧地将序列参数作为最终参数,因为这是Clojure中用于遍历序列的函数的约定。

答案 2 :(得分:1)

我之前(现已删除)的回答是不正确的,因为这并不像我初想的那么简单,这是我的第二次尝试:

(defn seq-replace
  [coll sub rep]
  (letfn [(seq-replace' [coll]
            (when-let [s (seq coll)]
              (let [start (take (count sub) s)
                    end (drop (count sub) s)]
                (if (= start sub)
                  (lazy-cat rep (seq-replace' end))
                  (cons (first s) (lazy-seq (seq-replace' (rest s))))))))]
    (seq-replace' coll)))