by Turing_Machine · 2y ago · 0 comments
(define scalar-sum +)
(define (+ . arguments)
(if (vectors? 'arguments)
(vector-sum 'arguments)
(apply scalar-sum arguments)))
vectors? and vector-sum not shown for clarity.
Edit: or maybe you want any sum in excess of (say) $5,000 to alert a manager. (define old-plus +)
(define (+ . args)
(let ((value (apply old-plus args)))
(if (> value 5000)
(begin
(alert-manager)
value)
value)))
The nice thing here is that this Just Works anywhere + appears in the code. You don't need to put the conditional and the call to alert-manager everywhere you're calculating a sum. Of course, in a real implementation you'd want to pass stuff to alert-manager like what module you're in, the transaction number, etc.