[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
reduce should allow an accessor function to be specified
- To: Mark Bromley <bromley@Think.COM>
- Subject: reduce should allow an accessor function to be specified
- From: STEVER%OZ.AI.MIT.EDU@xx.lcs.mit.edu
- Date: Sat, 14 Nov 1987 15:26:00 -0000
- Cc: common-lisp@Think.COM
- In-reply-to: Msg of 14 Nov 1987 04:38-EST from Mark Bromley <bromley at Think.COM>
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.