ks (seq keys) | |
vs (seq vals) |
(defn zipmap | |
"Returns a map with the keys mapped to the corresponding vals." | |
{:added "1.0" | |
:static true} | |
[keys vals] | |
(loop [map {} | |
ks (seq keys) | |
vs (seq vals)] | |
(if (and ks vs) | |
(recur (assoc map (first ks) (first vs)) | |
(next ks) | |
(next vs)) | |
map))) |
user=> (zipmap [] [1])
{}
user=> (zipmap2 [] [1])
{nil 1}
I definitely think the former result makes more sense than the latter does.
If you are thinking that it would be better to have the calls to seq in the if condition, that would be at least a little bit slower than the current implementation, as it would have 2 additional calls to seq for every iteration through the loop.
Andy
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.