4clojure 题解 Map Construction

作者: admin 日期: 2017-12-29 15:21:11 人气: - 评论: 0

把两个序列合并成一个map,不允许使用zipmap

Write a function which takes a vector of keys and a vector of values and constructs a map from them.

test not run
(= (__ [:a :b :c] [1 2 3]) {:a 1, :b 2, :c 3})
test not run
(= (__ [1 2 3 4] ["one" "two" "three"]) {1 "one", 2 "two", 3 "three"})
test not run
(= (__ [:foo :bar] ["foo" "bar" "baz"]) {:foo "foo", :bar "bar"})









用loop实现很简单


(fn [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)))




相关内容

发表评论
更多 网友评论0 条评论)
暂无评论

Copyright © 2012-2014 我的代码板 Inc. 保留所有权利。

页面耗时0.0285秒, 内存占用1.83 MB, 访问数据库13次

闽ICP备15009223号-1