Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destructuring Maps in clojure -- unused keys

I have defined a function which takes a map. I thought to use destructuring to access the values. However, I also want to check whether there are any used keys.

So, for example something like...

 (defun func1 [{:keys [a b c] :rest rest}]
    (println a b c)
    (println rest))

 (func1 {:a 1 :b 2 :c 3 :d 4})

which would print

 1 2 3
 4

The reason that I want this is that if rest is not null, this is probably an error, which I'd like to signal. I know about :as, which I could use. But then I need to store the list of valid keys twice.

Am I missing something?

Phil

like image 912
Phil Lord Avatar asked Nov 03 '12 14:11

Phil Lord


2 Answers

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.

like image 174
Cubic Avatar answered Nov 10 '22 14:11

Cubic


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}
like image 30
nha Avatar answered Nov 10 '22 14:11

nha