我想創建嵌套地圖mermaid圖這樣的Clojure - 變換嵌套地圖
{"a" {"b" {"c" nil
"d" nil}}
"e" {"c" nil
"d" {"h" {"i" nil
"j" nil}}}}
我覺得應該先轉換嵌套的地圖這種形式。那麼它應該很容易。
[{:out-path "a" :out-name "a"
:in-path "a-b" :in-name "b"}
{:out-path "a-b" :out-name "b"
:in-path "a-b-c" :in-name "c"}
{:out-path "a-b" :out-name "b"
:in-path "a-b-d" :in-name "d"}
{:out-path "e" :out-name "e"
:in-path "e-f" :in-name "f"}
{:out-path "e" :out-name "e"
:in-path "e-c" :in-name "c"}
{:out-path "e" :out-name "e"
:in-path "e-d" :in-name "d"}
{:out-path "e-d" :out-name "d"
:in-path "e-d-h" :in-name "h"}
{:out-path "e-d-h" :out-name "h"
:in-path "e-d-h-i" :in-name "i"}
{:out-path "e-d-h" :out-name "h"
:in-path "e-d-h-j" :in-name "j"}]
編輯:
這是我創造。但我完全不知道如何添加結果地圖的路徑。
(defn myfunc [m]
(loop [in m out []]
(let [[[k v] & ts] (seq in)]
(if (keyword? k)
(cond
(map? v)
(recur (concat v ts)
(reduce (fn [o k2]
(conj o {:out-name (name k)
:in-name (name k2)}))
out (keys v)))
(nil? v)
(recur (concat v ts) out))
out))))
你需要用代碼的具體幫助你試過了嗎?你可以發佈嗎? – Bill
@Bill,我需要幫助爲每個項目添加「路徑」。 – Ribelo
通常,將當前級別的路徑聲明(讓)到一個變量中,然後將其添加到正在構建的結構中,然後在遞歸到下一個級別並繼續構建它時將其傳遞給它。在這種情況下,多元函數可能更容易構造,而不是循環。 – Bill