Clojure:删除嵌套数据结构中所有nil项的最简单方法是什么?

时间:2013-09-13 17:36:59

标签: clojure null

假设我们有这个嵌套的矢量:

(def coll [nil [["this" nil "this"] nil] nil "this"])

您如何设计remove-nil函数以便所有nil消失?

(remove-nil coll)
;> [[["this" "this"]] "this"]

1 个答案:

答案 0 :(得分:5)

(clojure.walk/postwalk #(if (coll? %) (into (empty %) (remove nil? %)) %) coll)
;=> [[["this" "this"]] "this"]