0

我在 Luminus/Compojure 项目中有这个:

(defn article-show-single [id]
  (let [a (db/get-single-article {:id id})]
  (layout/render "show.html"
    {:article a}))

现在我想对:body一篇文章进行预处理。我可以这样做:

(str/replace (:body a) #"regex123"
           (fn [[_ var1 var2]]
               (str "new str 123")))
; => new str 123

但是如何将这两个结合起来,即我想改变:body一篇文章的,仍然返回文章。我怎样才能做到这一点?

4

1 回答 1

0

As the first step I would extract your preprocessing code as a function to make the code more readable:

(defn preprocess [s]
  (str/replace s
               #"regex123"
               (fn [[_ var1 var2]]
                 (str "new str 123"))))

Then I would use update function to update the value of a map's key by applying a provided function to the current value and use that value in the new version of the map:

(update article :body preprocess)
于 2016-06-04T05:20:58.533 回答