[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
reduce should allow an accessor function to be specified
Date: Saturday, 14 November 1987 04:38-EST
From: Mark Bromley <bromley at Think.COM>
[request for :KEY argument to REDUCE]
Summing the cars of a sequence of conses is cumbersome using
(reduce #'(lambda (a b)
(+ (if (consp a) (car a) a)
(if (consp b) (car b) b))) sequence)
Could you clean this up by using:
(reduce #'(lambda (total cons) (+ total (car cons)))
It's still not pretty, but it's a bit closer to 'nice'. It treats
empty sequences differently, though. My version returns 0 (the
:INITIAL-VALUE argument), while the other signals an error (the LAMBDA is
called with 0 arguments).
It would be much more natural to be able to use the following
(reduce #'+ sequence :key #'car).
I agree. :KEY is a much more elegant way to solve the problem.