[commodity-utilities] compact gnc:sum-collector-commodity

This function converted to purely functional approach.

Note that commodity-collectors always round amounts to the currency
SCU therefore we replicate the behaviour here.
This commit is contained in:
Christopher Lam 2019-07-05 23:09:19 +08:00
parent dd1873eb05
commit e783335cf2

View File

@ -924,21 +924,15 @@ construct with gnc:make-gnc-monetary and gnc:monetary->string instead.")
(define (gnc:sum-collector-commodity foreign domestic exchange-fn)
(and foreign
exchange-fn
(let ((balance (gnc:make-commodity-collector)))
(foreign
'format
(lambda (curr val)
(if (gnc-commodity-equiv domestic curr)
(balance 'add domestic val)
(balance 'add domestic
(gnc:gnc-monetary-amount
;; BUG?: this bombs if the exchange-fn
;; returns #f instead of an actual
;; <gnc:monetary>. Better to just return #f.
(exchange-fn (gnc:make-gnc-monetary curr val)
domestic)))))
#f)
(balance 'getmonetary domestic #f))))
(gnc:make-gnc-monetary
domestic
(apply + (map
(lambda (mon)
(gnc-numeric-convert
(gnc:gnc-monetary-amount (exchange-fn mon domestic))
(gnc-commodity-get-fraction domestic)
GNC-RND-ROUND))
(foreign 'format gnc:make-gnc-monetary #f))))))
;; As above, but adds only the commodities of other stocks and
;; mutual-funds. Returns a commodity-collector, (not a <gnc:monetary>)