Filter nil values from Clojure map?

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

    I would use:

    (into {} (filter (comp some? val) {:a :x, :b nil, :c :z}))
    
    => {:a :x, :c :z}
    

    Doing the some? check explicitly is important because if you just do (into {} (filter val {...})) then you will erroneously remove values that are boolean false.

提交回复
热议问题