如何遍历 clojure 中的地图列表并将其连接到字符串
how to iterate over a list of maps in clojure and concatenate that to a string
我有一个地图列表,其中包含两个键:path 和 :size
listOfMaps ({:path "a " :size "1 "}{{:path "b " :size " 2"}...)
如何迭代它并将它的路径和大小连接到一个字符串,以便它介于两者之间
即
str "initial" "a" "1" "b" "2" .... "end"
即通过循环填充的路径和大小应介于字符串 "initial" 和 "end"
之间
也许它很重,但我觉得它很有趣
(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)))
(apply str
`("initial"
~@(mapcat (juxt :path :size) list-of-maps)
"end"))
我有一个地图列表,其中包含两个键:path 和 :size
listOfMaps ({:path "a " :size "1 "}{{:path "b " :size " 2"}...)
如何迭代它并将它的路径和大小连接到一个字符串,以便它介于两者之间
即
str "initial" "a" "1" "b" "2" .... "end"
即通过循环填充的路径和大小应介于字符串 "initial" 和 "end"
之间也许它很重,但我觉得它很有趣
(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)))
(apply str
`("initial"
~@(mapcat (juxt :path :size) list-of-maps)
"end"))