Filter nil values from Clojure map?

后端 未结 5 2120
遥遥无期
遥遥无期 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:29

    (into {} (keep (fn [e] (if (val e) e)) {:a :x :b nil :c :z}))
    ;;=> {:a :x, :c :z}
    

    or a little shorter:

    (into {} (keep #(if (val %) %) {:a :x :b nil :c :z}))
    

    In fact, your filter suggestion is much better and shorter, so I would just use that:

    (into {} (filter val {:a :x :b nil :c :z}))
    

提交回复
热议问题