1

这是一个名为 args 的假设 hashmap:

{:body {:milestones [{:status 1 :otherValues x} 
                     {:status 2 :otherValues z} 
                     {:status 1 :otherValues y]}}

我的目标是收集每个 :status 键的值。它们都处于相同的深度,是 :milestones 的子级。

我越来越近了。我知道如何通过这样做来检索第一个状态的值:

(let [{[{:keys [status]} x] :milestones} :body} args]
  (println status))

最远的目标是找出哪些地图包含一个值为 1 的 :status 并为每个单独的地图创建一个新集合。

其实际应用是连接到 TeamworkPM 并使用 Google 日历同步具有“迟到”或“未完成”状态的里程碑。

在这种情况下,所需的输出将是 {1, 2, 1}。最终目标是拥有

 {{:status 1 :otherValues x} 
  {:status 1 :otherValues Y}}
4

1 回答 1

1

虽然我不知道如何直接将 map 的向量解构为变量,但是您可以先获取 的子级:milestones,然后使用基本的mapfilter.

请注意,您可以通过将其应用为函数来获取 map 的值。(例如,如果m{:key1 "val1"}(m :key1)将是"val1"

(def args {:body {:milestones [{:status 1 :otherValues 'x}
                               {:status 2 :otherValues 'z}
                               {:status 1 :otherValues 'y}]}})

(let [{{x :milestones} :body} args,
        y (map #(% :status) x),
        z (filter #(= (% :status) 1) x)
      ]
      (println x) ; [{:status 1, :otherValues x} {:status 2, :otherValues z} {:status 1, :otherValues y}]
      (println y) ; (1 2 1)
      (println z) ; ({:status 1, :otherValues x} {:status 1, :otherValues y})
  )
于 2014-06-06T00:01:55.463 回答