Destructuring Maps in clojure -- unused keys
I don't really understand why you'd ever want to know if there are things that you don't care about anyways. If you're trying to do something like "do something specific with these keys, and do something generic with the others" you could do something like:
(defn func [& {:keys [a b] :as args}]
(println a b c)
(println (dissoc args :a :b)))
(func :a 3 :b :c 5) =>
3 4
{:c 5}
nil
If you are paranoid about having to mention the keywords twice, you can probably do something about that too, but I can't imagine that it would be worth the bother.
The reason that I want this is that if rest is not null, this is probably an error, which I'd like to signal.
If you're that concerned about users passing in not exactly what you want, maybe a map isn't the right data structure to use.
If you care about enforcing a structure to a given map, Schema
may be a good choice (first example from the README
) :
(ns schema-examples
(:require [schema.core :as s
:include-macros true ;; cljs only
]))
(def Data
"A schema for a nested data type"
{:a {:b s/Str
:c s/Int}
:d [{:e s/Keyword
:f [s/Num]}]})
(s/validate
Data
{:a {:b "abc"
:c 123}
:d [{:e :bc
:f [12.2 13 100]}
{:e :bc
:f [-1]}]})
;; Success!
(s/validate
Data
{:a {:b 123
:c "ABC"}})
;; Exception -- Value does not match schema:
;; {:a {:b (not (instance? java.lang.String 123)),
;; :c (not (integer? "ABC"))},
;; :d missing-required-key}