2014-02-18 42 views
0

的名單我有兩個數據結構:結合地圖和矢量和列表

(def epics {"ARP-37" 8.0, "ARP-24" 5.0, "ARP-22" 13.0, "ARP-6" 21.0, "ARP-1" 8.0}) 
(def releases '(["Release one" '("ARP-37" "ARP-22" "ARP-6")])) 
; gets the sum of a list of epics (maps epic keys to its corresponding story points) 
(defn get-max-sp [epic-list] (reduce + (map #(get epics %) epic-list))) 
(def initial-sp (get-max-sp '("ARP-37" "ARP-22" "ARP-6"))) 

有些人可能會認識到這是JIRA數據。不過,我想這兩個結構結合起來,看起來就像這樣:

; where (now) is a function from clj-time returning the current date 
(def result [{x: (now), y: initial-sp } 
      {x: (+ now (get epics "ARP-37")), y: (- initial-sp (get epics "ARP-37))} 
      {x: (+ now (get epics "ARP-37") (get epics "ARP-22")), 
        y: (- initial-sp (get epics "ARP-37") (get epics "ARP-22"))} 
      {x: (+ now (get epics "ARP-37") (get epics "ARP-22") 
        (get epics "ARP-6")), 
        y: (- initial-sp (get epics "ARP-37") (get epics "ARP-22") 
         (get epics "ARP-6"))} 
      ]) 

所以,在執行功能後,我想要的結果看起來像這樣:

[{x: 2014-02-18, y: 42} 
{x: 2014-02-26, y: 34} 
{x: 2014-03-11, y: 21} 
{x: 2014-04-01, y: 0} 
] 

我有麻煩組合和映射兩種結構得到我的結果,我很想得到關於如何處理這個問題Clojure中:-)

感謝一些提示, 斯文

更新:因爲它似乎不清楚x和y應該是什麼,我會試着解釋他們多一點。 x應該是一個具有今天初始值的日期,並且在每個迭代步驟中,史詩的故事點將被添加到它中。「ARP-37」是史詩和8.0的故事點。 是類似的,它開始於史詩列表的所有故事點的一些,然後在每個迭代步驟下降其史詩故事點的數量。 迭代將在史詩列表上。

回答

2

我使用reductions來獲取累計值,因爲我們減少了您的輸入。

輸入源自使用map的史詩列表。

我使用一些簡單的類型轉換來獲得正確的增量和輸出類型。

(let [add-days (fn [d days] 
       (java.util.Date. (long (+ (.getTime d) 
              (* 1000 60 60 24 days))))) 
     initial {:x (java.util.Date.) 
       :y 42} 
     our-epics ["ARP-37" "ARP-22" "ARP-6"]] 
    (reductions (fn [tally epic-n] 
       {:x (add-days (:x tally) epic-n) 
       :y (long (- (:y tally) epic-n))}) 
       initial 
       (map #(get epics %) 
        our-epics))) 

({:x #inst "2014-02-18T22:31:17.027-00:00", :y 42} 
{:x #inst "2014-02-26T22:31:17.027-00:00", :y 34} 
{:x #inst "2014-03-11T22:31:17.027-00:00", :y 21} 
{:x #inst "2014-04-01T22:31:17.027-00:00", :y 0}) 
+0

這很漂亮:-)我不知道減少功能,感謝你的洞察力。 – sveri

0

我對JIRA數據並不熟悉,但我不完全清楚你想要的數據結構應該包含什麼。 x和y應該是什麼?

一般來說,將兩個或多個數據結構組合在一起並生成不同形狀的單個數據結構的方法是從連續簡化的角度考慮。首先嚐試以任何可能的方式合併兩個結構,將相關字段關聯到合適的關鍵字(減少一個或另一個結構,傳入空映射作爲初始值將是一個好的起點)。一旦你有了一個你不想要的形狀的單一數據結構,就可以通過一張地圖來傳遞它,或者再次減少以獲得更簡單的結果。重複,直到你有你想要的。如果您的最終解決方案太複雜,請確保它已經過測試,現在可以重構。

+0

嗨,我添加了一個更新,希望使這更清楚。我試過已經停止接近一個解決方案,但在過去幾個小時失敗,謝謝你再次激勵我,我明天會再試一次:-) – sveri