Destroying card cards with unknown keys

As an example, I have the following:

(def _t2 {:oxnard {:value-type "string" :rec-name "foo"}})

where is the :oxnard dynamic and unknown a-priori for the function , and the contained map consists of known key names (for example, :value-typeand :rec-name).

I am trying to write a function with destructuring without knowing the keyword of the external map, for example:

(defn if-foo? [ignoremapfirstkey & 
  {:keys [value-type rec-name]}] 
  (= rec-name "foo"))

or similar; however, it seems that I cannot get around the foreign key name that is not known.

+3
source share
2 answers

Assuming your function is passed as a argument with a dynamic key, you can use it to retrieve the inner map, which then can use it to retrieve the inner map and destroy it:

(let [{:keys [foo bar]} (get outer-map :inner-key)]
  ...)

;; k could be a function argument or Var rather than a let local
(let [k :inner-key
      {k {:keys [foo bar]}} outer-map]
  ...)

, , first val seq :

(let [{:keys [foo bar]} (val (first outer-map))]
  ...)

(let [[[_ {:keys [foo bar]}]] (seq outer-map)]
  ...)

(seq outer-map) ([k v]) (seq, ); seq, , _ , , {:keys [foo bar]} . seq , .

+5

?

(defmacro somekey
  [complex-map]
  (let [k (first (keys complex-map))]    ; get key from the outer map
    `(let [{inner-map# ~k} ~complex-map] ; use it for destructuring
        inner-map#)))                    ; make this as complex as you want

, , downvotes, , , . , , . (, , , .)

0

All Articles