How can I get the nested keys of a map in clojure?

后端 未结 11 1156
梦如初夏
梦如初夏 2020-12-15 10:59

if my structure is

{ :a :A
  :b :B
  :c {
       :d :D
     }
  :e {
       :f {
            :g :G
            :h :H
          }
     }
}

I

11条回答
  •  别那么骄傲
    2020-12-15 11:35

    Here is an implementation which returns all keys (not just the terminal keys) based on lazy-seq:

    (defn keys-in
      ([m] (if (map? m) (keys-in (seq m) [])))
      ([es c]
       (lazy-seq
        (when-let [e (first es)]
          (let [c* (conj c (key e))]
            (cons c* (concat (if (map? (val e)) (keys-in (seq (val e)) c*))
                             (keys-in (rest es) c))))))))
    

提交回复
热议问题