Skip to content

Instantly share code, notes, and snippets.

@luxbock luxbock/anonymous-gist.clj
Last active Aug 29, 2015

Embed
What would you like to do?
destr
;; Given that:
(destructure
'[[f [a b] {:keys [c d]} & rs]
[:first [:a 1 :b 2] {:c "C" :d "D"} 3 4 5]])
;; =>
[vec__22487 [:first [:a 1 :b 2] {:c "C", :d "D"} 3 4 5]
f (clojure.core/nth vec__22487 0 nil)
vec__22488 (clojure.core/nth vec__22487 1 nil)
a (clojure.core/nth vec__22488 0 nil)
b (clojure.core/nth vec__22488 1 nil)
map__22489 (clojure.core/nth vec__22487 2 nil)
map__22489 (if (clojure.core/seq? map__22489) (clojure.lang.PersistentHashMap/create (clojure.core/seq map__22489)) map__22489)
d (clojure.core/get map__22489 :d)
c (clojure.core/get map__22489 :c)
rs (clojure.core/nthnext vec__22487 3)]
;; I'm trying to simulate function argument destructuring.
;; --
;; This works:
(defn destructure-args [alist as]
(let [bs (destructure (vector alist as))
syms* (take-nth 2 (drop 2 bs))
vals (take-nth 2 (drop 3 bs))
syms (remove gensym? syms*)]
(into {}
(remove (fn [[k v]] (gensym? k)))
(zipmap syms* (eval `(let ~bs [~@vals]))))))
(destructure-args
'[f [a b] {:keys [c d]} & rs]
'[:first [:a 1 :b 2] {:c "C" :d "D"} 3 4 5])
;; =>
{rs (3 4 5), c "C", d "D", b 1, a :a, f :first}
;; But feels a bit hackish. Is there a better way?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.