Last active
August 29, 2015 14:13
-
-
Save luxbock/626e8d6192491564c6bf to your computer and use it in GitHub Desktop.
destr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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