我试图使用递归来遍历Clojure中任意嵌套的向量和映射树,并返回一个只包括关键字(包括顶部)的向量。以下示例数据应该返回:
请问是否有人可以帮我正确地实现这个功能? 以下是我目前的代码。
[:top :top :top :top :top :top :top :top :bottom :bottom :bottom :bottom :bottom :bottom :bottom :bottom :bottom :bottom :bottom :bottom]
,但没有特定的顺序。请问是否有人可以帮我正确地实现这个功能? 以下是我目前的代码。
(def sample [{:top {:top {:top [:bottom {:top {:top [:bottom :bottom :bottom]}} :bottom :bottom :bottom]}}},
{:top {:top [:bottom :bottom :bottom]}},
{:top [:bottom :bottom]}])
(defn make-flat [graph]
(loop [graph graph]
(if (every? keyword? graph) graph
(recur (into graph (flatten (seq (first (filter #(not (keyword? %)) graph)))))))))
(make-flat sample)