# Destructure map keys, and also rest of the map?

**URL:** https://clojureverse.org/t/destructure-map-keys-and-also-rest-of-the-map/3988
**Category:** How to?
**Created:** [March 14, 2019, 4:11pm UTC](https://clojureverse.org/t/destructure-map-keys-and-also-rest-of-the-map/3988 "2019-03-14T16:11:41Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![orestis](https://clojureverse.org/user_avatar/clojureverse.org/orestis/32/279_2.png) [@orestis](https://clojureverse.org/u/orestis)
#### Post date: [March 14, 2019, 4:11pm UTC](https://clojureverse.org/t/destructure-map-keys-and-also-rest-of-the-map/3988/1 "2019-03-14T16:11:42Z")

</div>

When doing associative destructuring, you often do:

`(defn [{:keys [foo bar]}] ...)`

Sometimes you do:

`(defn [{:keys [foo bar] :as arg}] ...)`

Which will give you the original argument under `arg`.

Recently, I’ve found a very common pattern when doing React development, in that I want to take the props I’m interested it from an argument, then pass that argument through to some other place, with the props dissoc’ed. I end up doing:

```auto
(defn [{:keys [foo bar] :as props}]
 (let [props (dissoc props :foo :bar)]
   [x props]))

```

I was wondering how hard would it be to also support something like:

```auto
(defn [{:keys [foo bar] :& props}]
 [x props])

```

I’m not sure whether this makes sense to put in clojure core, but I wonder if there is prior art that does similar things out there? So I could write my own destructuring macro that delegates the heavy lifting to the core, but then do some extra processing for the `:&` case.

---

<div class="post-metadata">

### Author: ![madbonkey](https://clojureverse.org/user_avatar/clojureverse.org/madbonkey/32/575_2.png) [@madbonkey](https://clojureverse.org/u/madbonkey)
#### Post date: [March 14, 2019, 4:21pm UTC](https://clojureverse.org/t/destructure-map-keys-and-also-rest-of-the-map/3988/2 "2019-03-14T16:21:20Z")

</div>

I‘ve also come across this back when JS got destructuring. You can get used to both quite easily, and different patterns emerge for both.

I mostly see this in React tutorials/docs/libs, when passing down props to child components people rely on destructuring working the JS way. It fits the ecosystem because React goes against Clojure/Script‘s open maps principle, and warns about everything that can‘t appear as props.

I‘d suggest working with the language here. Idiomatic Clojure code shouldn‘t care about additional data in maps, or at least as little as possible.

---

<div class="post-metadata">

### Author: ![orestis](https://clojureverse.org/user_avatar/clojureverse.org/orestis/32/279_2.png) [@orestis](https://clojureverse.org/u/orestis)
#### Post date: [March 14, 2019, 4:39pm UTC](https://clojureverse.org/t/destructure-map-keys-and-also-rest-of-the-map/3988/3 "2019-03-14T16:39:39Z")

</div>

You hit the nail on the head – React complains, and I want to silence the complains.

At the same time, I don’t think this is in the realm of the “open map” principle, because this isn’t data – these are essentially function arguments, and the presence of something unexpected there is most often a programmer error.

---

<div class="post-metadata">

### Author: ![madbonkey](https://clojureverse.org/user_avatar/clojureverse.org/madbonkey/32/575_2.png) [@madbonkey](https://clojureverse.org/u/madbonkey)
#### Post date: [March 14, 2019, 5:02pm UTC](https://clojureverse.org/t/destructure-map-keys-and-also-rest-of-the-map/3988/4 "2019-03-14T17:02:34Z")

</div>

Maybe I can phrase it more accurately. The open map principle (or, in Clojure it really is the open principle) is one of the reasons destructuring works the way it does. I can‘t speak to why it works like it does in JS, but there it fits nicely with React props (which are in JS and CLJS a map of data, it’s the entire point).

I think one of the reasons why React validates props in a closed way is because it also wants to be conforming to the HTML spec (it warns about nested form elements etc as well). There have even been some issues around including all the HTML validation in React, you can read some interesting discussions around that.

If you ask me, `select-keys` goes a long way here. I agree with you that it’s a pain point, the React interop and destructuring, but it’s not like there’s no tools. Even in JS land you‘d have to use the `prop-types` library for your custom stuff.

Edit: In general I would much rather have the default behavior of Clojure. You can always get rid of extra data in the leafs, but you can‘t get stuff back you removed eighteen middlewares or parent components back.

---

<div class="post-metadata">

### Author: ![didibus](https://clojureverse.org/user_avatar/clojureverse.org/didibus/32/951_2.png) [@didibus](https://clojureverse.org/u/didibus)
#### Post date: [March 14, 2019, 6:19pm UTC](https://clojureverse.org/t/destructure-map-keys-and-also-rest-of-the-map/3988/5 "2019-03-14T18:19:49Z")

</div>

I mean, I don’t know how often you get this pattern. But it doesn’t seem a big pain point.

Can’t you just:

```auto
(defn [props]
  [x (dissoc props :foo :bar)])

```

Otherwise, you could write a weird let macro, or some convenience function.

You could monkeypatch `clojure.core/destructure`, if you really wanted to bake it in the core destructuring logic, at your own risk.

---

<div class="post-metadata">

### Author: ![orestis](https://clojureverse.org/user_avatar/clojureverse.org/orestis/32/279_2.png) [@orestis](https://clojureverse.org/u/orestis)
#### Post date: [March 14, 2019, 6:32pm UTC](https://clojureverse.org/t/destructure-map-keys-and-also-rest-of-the-map/3988/7 "2019-03-14T18:32:33Z")

</div>

Yes, that’s what I’m doing now, but I’m also destructuring foo and bar, so I have to have the keys typed twice (first as symbols, then as keywords).

Thanks for the pointer to destructure, I’ll see if I can play around with that.

---

<div class="post-metadata">

### Author: ![teodorlu](https://clojureverse.org/user_avatar/clojureverse.org/teodorlu/32/2896_2.png) [@teodorlu](https://clojureverse.org/u/teodorlu)
#### Post date: [March 14, 2019, 9:01pm UTC](https://clojureverse.org/t/destructure-map-keys-and-also-rest-of-the-map/3988/8 "2019-03-14T21:01:11Z")

</div>

I made a little macro. Feedback, @orestis, @didibus ? This may be the weird let macro you suggested. Naming and API could use some work.

```clojure
(def my-thing {:x 99 :y 101 :message "Hah! Gotcha!"})

(let-extract [[x y] remainder my-thing]
    (prn x y)
    ;; prints 99 101
    (prn remainder)
    ;; prints {:message "Hah! Gotcha!"}
    )

```

Implementation has some limitations:

- No supporting multiple bindings (for now)
- Can’t co-exist in the same let as other things

… aaand it could use some refactoring.

> **Implementation**
>
> ```clojure
> (defn extract-keys*
> "Extract a selection of keys and keep the leftovers"
> [m selection]
> [(select-keys m selection)
> (apply dissoc m selection)]
> )
> 
> (defn extract-keys**
> [m selection restname]
> (let [[primary secondary] (extract-keys* m selection)]
> (assoc primary restname secondary)))
> 
> (defn let-extract* [[selection restname v] code]
> `(let [{:keys ~(conj selection restname)}
> (extract-keys** ~v ~(mapv keyword selection) ~(keyword restname))]
> ~@code))
> 
> (defmacro let-extract [bindings & code]
> (let-extract* bindings code))
> 
> (comment
> ;; See the macro expansion
> (-> '(let-extract [[x y] remainder my-thing]
> (prn x y)
> (prn remainder))
> macroexpand-1
> clojure.pprint/pprint)
> 
> ;; Test that it works
> (def my-thing {:x 99 :y 101 :message "Hah! Gotcha! Leaking some stuff!"})
> 
> (let-extract [[x y] remainder my-thing]
> (prn x y)
> ;; prints 99 101
> (prn remainder)
> ;; prints {:message "Hah! ..."}
> )
> )
> 
> ```

---

<div class="post-metadata">

### Author: ![miridius](https://clojureverse.org/user_avatar/clojureverse.org/miridius/32/1531_2.png) [@miridius](https://clojureverse.org/u/miridius)
#### Post date: [March 15, 2019, 12:21pm UTC](https://clojureverse.org/t/destructure-map-keys-and-also-rest-of-the-map/3988/9 "2019-03-15T12:21:50Z")

</div>

I don’t have much experience in pure React / JS land but I feel like you could avoid this problem by having your props map match the structure of your UI.

So instead of having all your props in a flat map (I hope you at least namespaced them!) like this:

```
(defn x [{:x/keys [foo bar] :as props}]
  (let [props' (dissoc props :foo :bar)]
    [y props']))

(defn y [{:y/keys [baz etc] :as props}]
  (let [props' (dissoc props :baz :etc)]
    [z props']))

```

Put the nested elements’ data inside a nested map:

```
(defn x [{:x/keys [foo bar y-props]}]
  [y y-props]))

(defn y [{:y/keys [baz etc z-props]}]
  [z z-props]))

```

Now this might pose a problem to get your data structure to match your UI structure, but that’s exactly where frameworks like Fulcro come in. Each component defines its own query, those queries are built together into a tree by the framework, and the data is coerced to match the query tree. Added bonus, the joined query is used to fetch data from the API and you only get the props you actually need in your data.

---

<div class="post-metadata">

### Author: ![didibus](https://clojureverse.org/user_avatar/clojureverse.org/didibus/32/951_2.png) [@didibus](https://clojureverse.org/u/didibus)
#### Post date: [March 15, 2019, 7:41pm UTC](https://clojureverse.org/t/destructure-map-keys-and-also-rest-of-the-map/3988/10 "2019-03-15T19:41:13Z")

</div>

Ya, something like that, though I was thinking simpler:

```auto
(defmacro without [ks _ as _ from & body]
  `(let [{:keys ~ks :as ~as} ~from
         ~as ~(apply dissoc from (map keyword ks))]
     ~@body))

(without [a b] :as m :from {:a 1 :b 2 :c 3}
         (println [a b])
         m)
;; Prints: [1 2]
;; Returns: {:c 3}

```

Now, if you want to support nested destructuring, string keys, vector destructuring, and everything else destructue supports, its going to get hairy. So I wouldn’t say this is a general purpose macro, but specific to the use case of OP, to use in the context of react.

---

<div class="post-metadata">

### Author: ![teodorlu](https://clojureverse.org/user_avatar/clojureverse.org/teodorlu/32/2896_2.png) [@teodorlu](https://clojureverse.org/u/teodorlu)
#### Post date: [March 15, 2019, 8:42pm UTC](https://clojureverse.org/t/destructure-map-keys-and-also-rest-of-the-map/3988/11 "2019-03-15T20:42:01Z")

</div>

Nice! Your solution was simpler indeed.

I agree with your point on generality, this is pleasant to be able to stick in the suitable namespace.

---

<div class="post-metadata">

### Author: ![TristeFigure](https://clojureverse.org/user_avatar/clojureverse.org/tristefigure/32/2076_2.png) [@TristeFigure](https://clojureverse.org/u/TristeFigure)
#### Post date: [March 16, 2019, 12:00am UTC](https://clojureverse.org/t/destructure-map-keys-and-also-rest-of-the-map/3988/12 "2019-03-16T00:00:02Z")

</div>

Since I had most of the parts under my hand, here it is.

First some imports and tooling to deal with destructuring forms:

```auto
(def ^:private reduce1 @#'clojure.core/reduce1)
(use 'clojure.pprint)

(declare disentangle)

(defn- disentangle-sequential [binding-form]
  (let [as (->> binding-form (drop-while #(not= % :as)) second)
        more (->> binding-form (drop-while #(not= % '&)) second)
        items (->> binding-form (remove (set (remove nil? [:as '& as more])))
                   vec)]
    (->> {:items items :as as :more more}
         (filter val)
         (into {}))))

(defn- disentangle-associative [binding-form]
  (let [as (binding-form :as)
        or (binding-form :or)
        ks (binding-form :keys)
        others (dissoc binding-form :as :or :keys)
        items (vec (distinct (concat ks (keys others))))
        mapping (merge (zipmap ks (map keyword ks))
                       others)]
    (->> {:items items :as as :or or :mapping mapping}
         (filter val)
         (into {}))))

(defn disentangle
  "Parses one level of destructuring.

  (disentangle '[a b & [c]])
  => '{:items [a b], :more [c]}

  (disentangle '{:keys [a] b :b [c1 c2] :c :or {d 1} :as m})
  => '{:items [a b [c1 c2]],
       :as m,
       :or {d 1},
       :mapping {a :a, b :b, [c1 c2] :c}}"
  [binding-form]
  (cond
    (or (sequential? binding-form) (nil? binding-form))
    ( disentangle-sequential binding-form)
    (map? binding-form)
    ( disentangle-associative binding-form)
    :else (throw (Exception. (str "Cannot disentangle " binding-form)))))

```

Wait, wait, wait, I forgot to pop a song: [https://www.youtube.com/watch?v=-JqFp6q8798](https://www.youtube.com/watch?v=-JqFp6q8798)

Ok, now, let’s rewrite clojure.core/destructure:

```auto
;; To highlight modifications ...
(defmacro ---HERE--------------------- [& body]
  `(do ~@body))

;; ...to the source code of clojure.core/destructure
;; Note: Here the goal is to support a :& key in destructuring maps.
(defn destructure& [bindings]
  (let [bents (partition 2 bindings)
        pb (fn pb [bvec b v]
             (let [pvec
                   (fn [bvec b val]
                     (let [gvec (gensym "vec__")
                           gseq (gensym "seq__")
                           gfirst (gensym "first__")
                           has-rest (some #{'&} b)]
                       (loop [ret (let [ret (conj bvec gvec val)]
                                    (if has-rest
                                      (conj ret gseq (list `seq gvec))
                                      ret))
                              n 0
                              bs b
                              seen-rest? false]
                         (if (seq bs)
                           (let [firstb (first bs)]
                             (cond
                              (= firstb '&) (recur (pb ret (second bs) gseq)
                                                   n
                                                   (nnext bs)
                                                   true)
                              (= firstb :as) (pb ret (second bs) gvec)
                              :else (if seen-rest?
                                      (throw (new Exception "Unsupported binding form, only :as can follow & parameter"))
                                      (recur (pb (if has-rest
                                                   (conj ret
                                                         gfirst `(first ~gseq)
                                                         gseq `(next ~gseq))
                                                   ret)
                                                 firstb
                                                 (if has-rest
                                                   gfirst
                                                   (list `nth gvec n nil)))
                                             (inc n)
                                             (next bs)
                                             seen-rest?))))
                           ret))))
                   pmap
                   (fn [bvec b v]
                     (let [gmap (gensym "map__")
                           gmapseq (with-meta gmap {:tag 'clojure.lang.ISeq})
                           defaults (:or b)]
                       (loop [ret (-> bvec (conj gmap) (conj v)
                                      (conj gmap) (conj `(if (seq? ~gmap) (clojure.lang.PersistentHashMap/create (seq ~gmapseq)) ~gmap))
                                      ((fn [ret]
                                         (let [ret (if (:as b)
                                                     (conj ret (:as b) gmap)
                                                     ret)
                                               ret (---HERE---------------------
                                                     (if (:& b)
                                                       (conj ret
                                                             (:& b)
                                                             `(dissoc ~gmap ~@(-> (select-keys b [:keys :syms :strs])
                                                                                  (assoc :inline (-> b disentangle :mapping (dissoc :&) vals))
                                                                                  (->> (mapcat (fn [[k vs]]
                                                                                                 (case k
                                                                                                   (:inline :keys) (map keyword vs)
                                                                                                   :sym (map #(do `(quote ~(symbol (name %)))) vs)
                                                                                                   :strs (map name vs))))
                                                                                       vec))))
                                                       ret))]
                                           ret))))
                              bes (let [transforms
                                          (reduce1
                                            (fn [transforms mk]
                                              (if (keyword? mk)
                                                (let [mkns (namespace mk)
                                                      mkn (name mk)]
                                                  (cond (= mkn "keys") (assoc transforms mk #(keyword (or mkns (namespace %)) (name %)))
                                                        (= mkn "syms") (assoc transforms mk #(list `quote (symbol (or mkns (namespace %)) (name %))))
                                                        (= mkn "strs") (assoc transforms mk str)
                                                        :else transforms))
                                                transforms))
                                            {}
                                            (keys b))]
                                    (reduce1
                                        (fn [bes entry]
                                          (reduce1 #(assoc %1 %2 ((val entry) %2))
                                                   (dissoc bes (key entry))
                                                   ((key entry) bes)))
                                        (dissoc b :as :or
                                                (---HERE---------------------
                                                  :&))
                                        transforms))]
                         (if (seq bes)
                           (let [bb (key (first bes))
                                 bk (val (first bes))
                                 local (if (instance? clojure.lang.Named bb) (with-meta (symbol nil (name bb)) (meta bb)) bb)
                                 bv (if (contains? defaults local)
                                      (list `get gmap bk (defaults local))
                                      (list `get gmap bk))]
                             (recur (if (ident? bb)
                                      (-> ret (conj local bv))
                                      (pb ret bb bv))
                                    (next bes)))
                           ret))))]
               (cond
                (symbol? b) (-> bvec (conj b) (conj v))
                (vector? b) (pvec bvec b v)
                (map? b) (pmap bvec b v)
                :else (throw (new Exception (str "Unsupported binding form: " b))))))
        process-entry (fn [bvec b] (pb bvec (first b) (second b)))]
    (if (every? symbol? (map first bents))
      bindings
      (reduce1 process-entry [] bents))))

```

Let’s test it out:

```auto
(let [destr (destructure& '[{:keys [a b] :& more} {:a 1 :b 2 :c 3 :d 4}])]
  (pprint destr) (newline)
  (comment
    [map__7307
     {:a 1, :b 2, :c 3, :d 4}
     map__7307
     (if
       (clojure.core/seq? map__7307)
       (clojure.lang.PersistentHashMap/create (clojure.core/seq map__7307))
       map__7307)
     more
     (apply clojure.core/dissoc map__7307 [:a :b])
     a
     (clojure.core/get map__7307 :a)
     b
     (clojure.core/get map__7307 :b)]))

```

Seems to work.

Now let’s wrap this behavior into a `let&` macro.

```auto
(defmacro let& [bindings & body]
  `(let ~(destructure& bindings)
     ~@body))

```

But are we going to use this raw, like cavemen ? No. We’re sophisticated people, we’re going to cook it with a macro. What should this macro do ? Allow for various binding styles to cohabit in the same binding vector.

```auto
(xlet [ a 1
       :binding *file* "."
       :with-open r (clojure.java.io/input-stream "myfile.txt")]
      body...)

```

should expand to

```auto
(let [a 1]
  (binding [*file* "."]
    (with-open [r (clojure.java.io/input-stream "myfile.txt")]
      body...)))

```

Here is the code for this:

```auto
(require '[clojure.spec.alpha :as s])

(s/def ::xlet-bindings
  (s/+ (s/cat
         :style (s/? keyword?)
         :binding-expr #(or (symbol? %) (map? %) (vector? %))
         :bound-expr any?)))

(defn xlet* [[{:keys [:style :binding-expr :bound-expr]} & more-bindings] body]
  `(~(or (some->> (some-> style str
                          (as-> $ (if (= (first $) \:)
                                    (rest $)
                                    $)))
                  (apply str) symbol)
         'clojure.core/let)
      [~binding-expr ~bound-expr]
      ~@(if (empty? more-bindings)
          body
          [(xlet* more-bindings body)])))

(defmacro xlet [bindings & body]
  (let [bds (s/conform ::xlet-bindings bindings)]
    (xlet* bds body)))

```

And to test the whole:

```auto
(xlet [the-map {:a 1 :b 2 :c [3] :d 4}
       :let& {:keys [a] bb :b [c] :c :& more} the-map]
  (println "the-map" the-map) (newline)
  (comment the-map {:a 1, :b 2, :c [3], :d 4})
  (println "[a bb c]" [a bb c]) (newline)
  (comment [1 2 3])
  (println "more" more) (newline)
  (comment more {:d 4}))

```

Gist: [https://gist.github.com/TristeFigure/822965f159eb5c578fb958cc301f070b](https://gist.github.com/TristeFigure/822965f159eb5c578fb958cc301f070b)

_She think she need me, girl you gon’ make it_  
_Drink got me leaning, trap got me faded_

---

<div class="post-metadata">

### Author: ![didibus](https://clojureverse.org/user_avatar/clojureverse.org/didibus/32/951_2.png) [@didibus](https://clojureverse.org/u/didibus)
#### Post date: [March 17, 2019, 10:39pm UTC](https://clojureverse.org/t/destructure-map-keys-and-also-rest-of-the-map/3988/13 "2019-03-17T22:39:20Z")

</div>

Nice attempt, I think there’s some bugs lurking though. For example this didn’t work:

```auto
(let& [{c :c
        b :b
        :as m
        {aa :aa
         :as nm} :a}
       {:a {:aa :aa :bb :bb} :b :b :c :c}]
  (println [aa b c])
  [m nm])

```

So does this one:

```auto
(let& [{:keys [b c] :& m
        {:keys [aa] :& nm} :a}
       {:a {:aa :aa :bb :bb} :b :b :c :c}]
  (println [aa b c])
  [m nm])

```

---

<div class="post-metadata">

### Author: ![TristeFigure](https://clojureverse.org/user_avatar/clojureverse.org/tristefigure/32/2076_2.png) [@TristeFigure](https://clojureverse.org/u/TristeFigure)
#### Post date: [March 18, 2019, 12:38pm UTC](https://clojureverse.org/t/destructure-map-keys-and-also-rest-of-the-map/3988/14 "2019-03-18T12:38:19Z")

</div>

Thanks for pointing it out, _destructure&_ was just missing a else case in a (if …) form I thought I had fixed. I updated my comment as well as the gist (where _xlet_ became _may_).

---

<div class="post-metadata">

### Author: ![system](https://clojureverse.org/uploads/default/original/2X/5/51079bf9e4b7d9466242c06cf1e43b9f8bd6da14.png) [@system](https://clojureverse.org/u/system)
#### Post date: [September 17, 2019, 12:38am UTC](https://clojureverse.org/t/destructure-map-keys-and-also-rest-of-the-map/3988/15 "2019-09-17T00:38:22Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
