get-in
用于访问嵌套的关联数据结构。
(def m {:a {:x 1}})
(get-in m [:a :x]) ;;=> 1
解构映射后,这些值在范围内并通过符号访问。您的示例应如下所示:
(def mapVal
{:test "n"
:anotherKey "n"})
(defn functionTest
[& {:keys [test anotherKey]
:or {:test "d" :anotherKey "d"}}]
(println test anotherKey))
(defn mapFunc
[m]
(apply functionTest (apply concat (select-keys m [:test :anotherKey]))))
(mapFunc mapVal) ;;=> prints: n n
您必须完成此操作,因为functionTest
它接受裸键值对作为可选参数(& 右侧的参数),如下所示:
(functionTest :test "n"
:anotherKey "n" )
;;=> Also prints: n n
select-keys
返回仅包含指定键的映射:
(select-keys mapVal [:test])
;; => {:test "n"}
应用于concat
映射返回键和值的平面序列:
(apply concat (select-keys mapVal [:test :anotherKey]))
;; => (:test "n" :anotherKey "n")
apply
将函数应用于 seq,就好像 seq 是它的参数列表:
(+ [1 2 3]) ;;=> Error.
(apply + [1 2 3]) ;; => 6
作为旁注,通常在 Clojure 代码中,对于大多数名称,蛇形大小写比骆驼大小写更受欢迎。