如何迭代clojure中的地图列表并将其连接到字符串

时间:2016-07-08 07:30:20

标签: clojure clojure-java-interop

我有一个包含两个键的地图列表:path和:size

 listOfMaps ({:path "a " :size "1 "}{{:path "b " :size " 2"}...)

如何迭代它并将其路径和大小连接到一个字符串,以便它位于

之间

 str "initial" "a" "1" "b" "2" .... "end" 

即通过循环填充的路径和大小应该在字符串之间" initial"和"结束"

2 个答案:

答案 0 :(得分:5)

(apply str
       `("initial"
         ~@(mapcat (juxt :path :size) list-of-maps)
         "end"))

答案 1 :(得分:0)

也许它很重但我觉得很有趣

(defn str-values [data]
  (as-> data d
        (map vec d)
        (flatten d)
        (remove keyword? d)
        (concat ["initial"] d ["end"])
        (apply str d)))

输出

"initiala1b2end"

这就是你想要的吗?

修改 由OlegTheCat更正

(defn str-values [data]
  (as-> data d
        (map (juxt :path :size) d)
        (flatten d)
        (remove keyword? d)
        (concat ["initial"] d ["end"])
        (apply str d)))