如何在当前地图clojure中向地图添加键/创建键

时间:2015-06-05 00:57:43

标签: clojure ring

我有来自get请求的以下地图

:form-params {"qu" ["2015 Q2" "2015 Q3" "2015 Q4" "2015 Q2"
                    "2015   Q3" "2015 Q4" "2015 Q2" "2015 Q3"
                    "2015 Q4" "2015 Q2" "2015 Q3" "2015 Q4"],
              "sh" ["2580" "2580" "2580" "2983"
                    "2983" "2983" "53" "53"
                    "53" "4558" "4558" "4558"],
              "prin" ["191" "191" "191" "191"
                      "191" "191" "191" "191"
                      "191" "191" "191" "191"],
              "bu" ["" "" "" "" "" "" "" "" "" "" "" ""]}

我想创建键:quarter,:sh,:prin和:bu。原因是从这里我将分成四组并交错,如下所示

 (let [params (:form-params request)
                   data (partition 4 (interleave (:quarter params) (:shiptoid params)
                                                 (:principalid params) (:budget params)
                                                  ))

谢谢!

1 个答案:

答案 0 :(得分:3)

内置函数clojure.set/rename-keys可以很好地完成这项工作,但是就像clojure中的所有内容一样,有几种方法。让我们来看看这个过程。你所拥有的是非常接近工作。如果我只是通过它的原始字符串键查找每个,我们可以看到它的工作原理:

user> (def request {:form-params {"qu" ["2015 Q2" "2015 Q3" "2015 Q4" "2015 Q2"
                                        "2015   Q3" "2015 Q4" "2015 Q2" "2015 Q3"
                                    "2015 Q4" "2015 Q2" "2015 Q3" "2015 Q4"],
                              "sh" ["2580" "2580" "2580" "2983"
                                    "2983" "2983" "53" "53"
                                    "53" "4558" "4558" "4558"],
                              "prin" ["191" "191" "191" "191"
                                      "191" "191" "191" "191"
                                      "191" "191" "191" "191"],
                              "bu" ["" "" "" "" "" "" "" "" "" "" "" ""]}})
#'user/request
user> (let [params (:form-params request)
            data (partition 4 (interleave (params "qu") (params "sh")
                                          (params "prin") (params "bu")))]
        (clojure.pprint/pprint data))
(("2015 Q2" "2580" "191" "")
 ("2015 Q3" "2580" "191" "")
 ("2015 Q4" "2580" "191" "")
 ("2015 Q2" "2983" "191" "")
 ("2015   Q3" "2983" "191" "")
 ("2015 Q4" "2983" "191" "")
 ("2015 Q2" "53" "191" "")
 ("2015 Q3" "53" "191" "")
 ("2015 Q4" "53" "191" "")
 ("2015 Q2" "4558" "191" "")
 ("2015 Q3" "4558" "191" "")
 ("2015 Q4" "4558" "191" ""))

现在听起来你想要一张与第一张地图一样的地图,除了它有不同的键指向相同的值。为此,我们可以轻松高效地使用地图文字创建这个新地图:

user> (def new-request
        (let [params (:form-params request)]
          {:form-params {:quarter (params "qu")
                         :shiptoid (params "sh")
                         :principalid (params "prin")
                         :budget (params "bu")}}))
#'user/new-request
user> (let [params (:form-params new-request)
            data (partition 4 (interleave (:quarter params) (:shiptoid params)
                                          (:principalid params) (:budget params)))]
        (clojure.pprint/pprint data))
(("2015 Q2" "2580" "191" "")
 ("2015 Q3" "2580" "191" "")
 ("2015 Q4" "2580" "191" "")
 ("2015 Q2" "2983" "191" "")
 ("2015   Q3" "2983" "191" "")
 ("2015 Q4" "2983" "191" "")
 ("2015 Q2" "53" "191" "")
 ("2015 Q3" "53" "191" "")
 ("2015 Q4" "53" "191" "")
 ("2015 Q2" "4558" "191" "")
 ("2015 Q3" "4558" "191" "")
 ("2015 Q4" "4558" "191" ""))

为了使其更加自动化,您可以使用update-in

user> (def new-request
        (update-in request [:form-params]
                   (fn [params] {:quarter (params "qu")
                                 :shiptoid (params "sh")
                                 :principalid (params "prin")
                                 :budget (params "bu")})))
#'user/new-request
user> (let [params (:form-params new-request)
            data (partition 4 (interleave (:quarter params) (:shiptoid params)
                                          (:principalid params) (:budget params)))]
        (clojure.pprint/pprint data))
(("2015 Q2" "2580" "191" "")
 ("2015 Q3" "2580" "191" "")
 ("2015 Q4" "2580" "191" "")
 ("2015 Q2" "2983" "191" "")
 ("2015   Q3" "2983" "191" "")
 ("2015 Q4" "2983" "191" "")
 ("2015 Q2" "53" "191" "")
 ("2015 Q3" "53" "191" "")
 ("2015 Q4" "53" "191" "")
 ("2015 Q2" "4558" "191" "")
 ("2015 Q3" "4558" "191" "")
 ("2015 Q4" "4558" "191" ""))

从clojure 1.7开始(截至今天的下一个版本)你也可以使用update函数,它稍微优雅一点。虽然通过添加clojure.set库并使用内置的rename-keys函数,仍然可以使这更加优雅:

user> (def new-request
        (update request :form-params
                clojure.set/rename-keys {"qu" :quarter
                                         "sh" :shiptoid
                                         "prin" :principalid
                                         "bu" :budget}))
#'user/new-request
user> (let [params (:form-params new-request)
            data (partition 4 (interleave (:quarter params) (:shiptoid params)
                                          (:principalid params) (:budget params)))]
        (clojure.pprint/pprint data))
(("2015 Q2" "2580" "191" "")
 ("2015 Q3" "2580" "191" "")
 ("2015 Q4" "2580" "191" "")
 ("2015 Q2" "2983" "191" "")
 ("2015   Q3" "2983" "191" "")
 ("2015 Q4" "2983" "191" "")
 ("2015 Q2" "53" "191" "")
 ("2015 Q3" "53" "191" "")
 ("2015 Q4" "53" "191" "")
 ("2015 Q2" "4558" "191" "")
 ("2015 Q3" "4558" "191" "")
 ("2015 Q4" "4558" "191" ""))