【问题标题】:how to iterate over a list of maps in clojure and concatenate that to a string如何遍历clojure中的映射列表并将其连接到字符串
【发布时间】:2016-07-08 07:30:19
【问题描述】:

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

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

如何对其进行迭代并将其路径和大小连接到一个字符串,使其介于两者之间

那是

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

通过循环填充的路径和大小应该在字符串“initial”和“end”之间

【问题讨论】:

    标签: clojure clojure-java-interop


    【解决方案1】:
    (apply str
           `("initial"
             ~@(mapcat (juxt :path :size) list-of-maps)
             "end"))
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • (map vec d) - 这是不正确的。你不能依赖映射键的顺序。
      • 我想使用(map (juxt :path :size) d)
      猜你喜欢
      • 1970-01-01
      • 2012-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2018-11-13
      • 2021-05-26
      • 1970-01-01
      相关资源
      最近更新 更多