我有對象的單一載體的載體到載體的載體,我想變換成對象,其中在所述子向量的每個對象具有特定成員的值相同的矢量的矢量。例如分裂基於關鍵
[{:sku "105"}, {:sku "105"}, {:sku "120"}]
成爲
[[{:sku "105"}, {:sku "105"}], [{:sku "120"}]]
我試圖把握思維功能性的方式,但我敢肯定,我現在還沒有,因爲我的代碼看起來很笨拙。但這裏是我到目前爲止有:
(defn separate-by-invoice [original-invoices]
(let [sorted-invoices (sort-by :invoice-number original-invoices)]
(def temp-invoices [])
(reduce (fn [final-invoices invoice]
(let [prev-invoice-number (get-in (last temp-invoices) [:invoice-number])
invoice-number (get-in invoice [:invoice-number])]
(if (= prev-invoice-number invoice-number)
(do
(into temp-invoices invoice))
(do
(into final-invoices temp-invoices)
(def temp-invoices [])
(into temp-invoices invoice))))
final-invoices)
[]
sorted-invoices)))
基本上,我的想法是,我形成一個矢量,溫度,發票,並與所有具有相同的發票編號的條目的填充,然後一旦我們得到所有這些,將該向量插入最終向量,然後返回該值。但是,似乎最終發票總是空的矢量。我究竟做錯了什麼?這是如何通常在clojure中完成的?
非常感謝。這比我想象的要容易得多。 – kennycoc