[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) (define (gnc:sum-collector-commodity foreign domestic exchange-fn)
(and foreign (and foreign
exchange-fn exchange-fn
(let ((balance (gnc:make-commodity-collector))) (gnc:make-gnc-monetary
(foreign domestic
'format (apply + (map
(lambda (curr val) (lambda (mon)
(if (gnc-commodity-equiv domestic curr) (gnc-numeric-convert
(balance 'add domestic val) (gnc:gnc-monetary-amount (exchange-fn mon domestic))
(balance 'add domestic (gnc-commodity-get-fraction domestic)
(gnc:gnc-monetary-amount GNC-RND-ROUND))
;; BUG?: this bombs if the exchange-fn (foreign 'format gnc:make-gnc-monetary #f))))))
;; 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))))
;; As above, but adds only the commodities of other stocks and ;; As above, but adds only the commodities of other stocks and
;; mutual-funds. Returns a commodity-collector, (not a <gnc:monetary>) ;; mutual-funds. Returns a commodity-collector, (not a <gnc:monetary>)