(def order-info-query
(->> (d/q
'[:find ?si ?ia ?os ?p
:where
[?e :proposal/id _]
[?e :proposal/supplier-id ?s]
[?e :proposal/item-amount ?ia]
[?e :proposal/amount ?p]
[?e :proposal/order-status ?ps]
[?s :company/brand-name ?si]
[?ps :db/ident ?os]
]
db)
)
)
我的查询输出没有单价,因此我需要计算它们并返回单价中包含的新数据。
(identity order-info-query)
;=>
;[["IBM" 10 :order-status/ongoing 110000]
; ["apple" 20 :order-status/waiting 239000]
; ["HP" 10 :order-status/ongoing 119000]]
您好,我想查找单价并将这些价格从便宜到贵排序。 这是我的代码,我想知道是否有更简单或棘手的方法来做到这一点?
(sort-by last (mapv (fn [x] (let [[n c v b] x] (conj [] n c v b "Unit price:" (/ b c)))) order-info-query))
;=>
;(["IBM" 10 :order-status/ongoing 110000 "Unit price:" 11000]
; ["HP" 10 :order-status/ongoing 119000 "Unit price:" 11900]
; ["apple" 20 :order-status/waiting 239000 "Unit price:" 11950])
您的订单信息向量开始变得足够长,因此使用映射可能是一个更好的主意,因此值具有解释它们是什么的键,然后您只需向每个映射添加一个新的键/值即可。
但在此之前,您可以简化添加单价文本和值的匿名函数。您可以使用
(conj [] n c v b …)
代替 (conj x …)
。那么你就不再需要解构n
或v
:(let [_ c _ b] …)