Filter nil values from Clojure map?

后端 未结 5 2118
遥遥无期
遥遥无期 2021-02-12 14:42

What is the best way to filter nil values from a Clojure map {}?

{ :a :x :b nil :c :z }
;;=>  { :a :x, :c :z }
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-12 15:19

    Probably not the best solution, but here's one that uses list comprehension:

    (into {} 
      (for [[k v] {:a 1 :b nil :c :z} :when (not (nil? v))]
        [k v]))
    

提交回复
热议问题