How to remove multiple keys from a map?

前端 未结 4 611
难免孤独
难免孤独 2020-12-29 20:13

I have a function that removes a key from a map:

(defn remove-key [key map]
  (into {}
        (remove (fn [[k v]] (#{key} k))
                map)))

(remov         


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-29 20:36

    As others said use the built-in function instead of writing your own.

    However, if this was just an example and you want an answer of how to do that if there wasn't a standard dissoc, then you can use:

    (reduce (fn [m k] (remove-key k m)) {:foo 1 :bar 2 :baz 3} [:foo :bar])
    

    Obviously, if you revert the arguments to remove-key it can be written much simpler:

    (reduce remove-key {:foo 1 :bar 2 :baz 3} [:foo :bar])
    

提交回复
热议问题