(apply f x args)
orlin opened this issue · comments
Ki's implementation of apply differs from Clojure. According to ClojureDocs apply
"applies fn f to the argument list formed by prepending intervening arguments to args". Only prepending. I'm interested in the second signature (apply f x args)
. Here is an example:
(def pick (js require("lodash").pick))
(defn pick1 [what key]
(pick what key))
(defn pickm [what keys]
(apply pick what keys))
(.log console (pick1 {$ "a" 1 "b" 2} "a")) // { a: 1 }
(.log console (pickm {$ "a" 1 "b" 2 "c" 3 } [$ "a" "b"])) // {}
(.log console (pick {$ "a" 1 "b" 2 "c" 3 } "a" "b")) // { a: 1, b: 2 }
The pick of lodash is same as underscore's and I don't think mori has an equivalent fn. If apply with three arguments worked like Clojure's apply, I believe the second console.log
would have matched the third. I'm not very skilled with lispy tricks, so I wrote my own pick like so:
(defn pick [from keys]
(let [res {$}]
(loop [key (.pop keys)]
(if (js key == undefined)
res
(do
(js res[key] = from[key])
(recur (.pop keys)))))))
It would be cool if ki functions / special forms named same as Clojure / ClojureScript worked the same.
I agree. It turns out that mori exposes apply already, so I removed the apply implementation from ki.sjs and things works as expected now. I released 0.2.1, feel free to give it a try.
Regarding pick
, I believe selectKeys
does the same, check it out.
Thanks for apply
and for pointing out selectKeys
, I didn't realize it existed because it's not listed in mori's docs. It doesn't work in this case though. I believe because I'm picking / selecting from json {$}
and selectKeys
probably expects edn maps. So apply + underscore is a win. Btw, apply's api docs should probably be updated...
Updated api docs, thanks for pointing this out.