Last active
December 8, 2016 08:55
-
-
Save joinr/60bf2f23429a3ffc9d4228fe9c3d6d06 to your computer and use it in GitHub Desktop.
broken cljs map destructuring
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
(defn broken | |
"We should be able to collect the args via destructuring, | |
implicitly merge them with defaults, and return the resulting | |
map aliased by m, right? This idiom works in clojure..." | |
[& {:keys [x y min max] :or | |
{x 0 y 0 min 0 max 0} :as m}] | |
m) | |
;;=>(broken) | |
;;nil | |
;;=>(broken :x 2) | |
;;{:x 2} | |
;;evaling broken in the cljs repl gives us: | |
;;#object[figdemo$bmi$broken "function figdemo$bmi$broken(var_args){ | |
;;var args__21376__auto__ = []; | |
;;var len__21369__auto___23263 = arguments.length; | |
;;var i__21370__auto___23264 = (0); | |
;;while(true){ | |
;;if((i__21370__auto___23264 < len__21369__auto___23263)){ | |
;;args__21376__auto__.push((arguments[i__21370__auto___23264])); | |
;;var G__23265 = (i__21370__auto___23264 + (1)); | |
;;i__21370__auto___23264 = G__23265; | |
;;continue; | |
;;} else { | |
;;} | |
;;break; | |
;;} | |
;;looks like none of the defaults via the :or bind are ever used.... | |
(defn working | |
"Lame work-around..." | |
[& opts] | |
(let [{:keys [x y min max] :or | |
{x 2 y 3 min 0 max 0}} (apply hash-map opts)] | |
{:x x :y y :min min :max max})) | |
;;=>(working) | |
;;{:x 2 :y 3 :min 0 :max 0} | |
;;=>(working :y 4) | |
;;{:x 2 :y 4 :min 0 :max 0} | |
(defn still-broken | |
"Looks like working, acts like broken...something is wrong with :as and :or, or | |
the behavior is significantly different from clojure." | |
[& opts] | |
(let [{:keys [x y min max] :or | |
{x 2 y 3 min 0 max 0} :as m} (apply hash-map opts)] | |
m)) | |
;;Update: the assumably idiomitic way to do this in cljs. | |
(defn weak-fix [& input] | |
(let [m (merge {:x 0 :y 0 :min 0 :max 0} | |
(apply hash-map input))] | |
m)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment