使用幽灵转换与键匹配的值
Using specter to transform values that match a key
如果此问题已在别处得到解答,我很抱歉,但我似乎无法找到与我正在寻找的模式相匹配的示例。我也可能还没有完全理解递归幽灵路径。
如果我有数据(明确使用嵌套向量):
{:a "1" :b "2" :c [ {:a "3" :b "4"} {:a "5" :b "6"} ]}
并且我想将 keyword
函数应用于所有具有键 :a
的值,结果是:
{:a :1 :b "2" :c [ {:a :3 :b "4"} {:a :5 :b "6"} ]}
最后,我希望它可以递归到任意深度,并处理向量情况。
我读过 https://github.com/nathanmarz/specter/wiki/Using-Specter-Recursively ,但我一定遗漏了一些重要的东西。
感谢任何为我指明正确方向的人!
不是 Spectre 解决方案,但可以通过 clojure.walk/postwalk
:
轻松完成
(ns demo.core
(:require
[clojure.walk :as walk] ))
(def data {:a "1" :b "2" :c [{:a "3" :b "4"} {:a #{7 8 9} :b "6"}]})
(def desired {:a :1 :b "2" :c [{:a :3 :b "4"} {:a #{7 8 9} :b "6"}]})
(defn transform
[form]
(if (map-entry? form)
(let [[key val] form]
(if (and
(= :a key)
(string? val))
[key (keyword val)] ; can return either a 2-vector
{key val})) ; or a map here
form))
(walk/postwalk transform data) =>
{:a :1, :b "2", :c [{:a :3, :b "4"} {:a #{7 9 8}, :b "6"}]}
我什至为其中一个 :a
值添加了一个非字符串,以使其更加棘手。
(use '[com.rpl.specter])
(let [input {:a "1" :b "2" :c [{:a "3" :b "4"} {:a "5" :b "6"}]}
desired-output {:a :1 :b "2" :c [{:a :3 :b "4"} {:a :5 :b "6"}]}
FIND-KEYS (recursive-path [] p (cond-path map? (continue-then-stay [MAP-VALS p])
vector? [ALL p]
STAY))]
(clojure.test/is
(= (transform [FIND-KEYS (must :a)] keyword input)
desired-output)))
如果此问题已在别处得到解答,我很抱歉,但我似乎无法找到与我正在寻找的模式相匹配的示例。我也可能还没有完全理解递归幽灵路径。
如果我有数据(明确使用嵌套向量):
{:a "1" :b "2" :c [ {:a "3" :b "4"} {:a "5" :b "6"} ]}
并且我想将 keyword
函数应用于所有具有键 :a
的值,结果是:
{:a :1 :b "2" :c [ {:a :3 :b "4"} {:a :5 :b "6"} ]}
最后,我希望它可以递归到任意深度,并处理向量情况。
我读过 https://github.com/nathanmarz/specter/wiki/Using-Specter-Recursively ,但我一定遗漏了一些重要的东西。
感谢任何为我指明正确方向的人!
不是 Spectre 解决方案,但可以通过 clojure.walk/postwalk
:
(ns demo.core
(:require
[clojure.walk :as walk] ))
(def data {:a "1" :b "2" :c [{:a "3" :b "4"} {:a #{7 8 9} :b "6"}]})
(def desired {:a :1 :b "2" :c [{:a :3 :b "4"} {:a #{7 8 9} :b "6"}]})
(defn transform
[form]
(if (map-entry? form)
(let [[key val] form]
(if (and
(= :a key)
(string? val))
[key (keyword val)] ; can return either a 2-vector
{key val})) ; or a map here
form))
(walk/postwalk transform data) =>
{:a :1, :b "2", :c [{:a :3, :b "4"} {:a #{7 9 8}, :b "6"}]}
我什至为其中一个 :a
值添加了一个非字符串,以使其更加棘手。
(use '[com.rpl.specter])
(let [input {:a "1" :b "2" :c [{:a "3" :b "4"} {:a "5" :b "6"}]}
desired-output {:a :1 :b "2" :c [{:a :3 :b "4"} {:a :5 :b "6"}]}
FIND-KEYS (recursive-path [] p (cond-path map? (continue-then-stay [MAP-VALS p])
vector? [ALL p]
STAY))]
(clojure.test/is
(= (transform [FIND-KEYS (must :a)] keyword input)
desired-output)))