4 ms·
One that comes immediately to mind is that you might want + to handle vectors as well as scalars. Something like: (define scalar-sum +) (define (+ . a
by Turing_Machine 3y ago
One that comes immediately to mind is that you might want + to handle vectors as well as scalars. Something like:
(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.
- taeric 3y agoFair, I should have tried to use my imagination there. Basically operator overloading? I forgot that generic functions are different beasts, and I've rarely had need for this, myself.