[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


In general I favor this proposal, but it needs some cleaning up before I'm happy
with it.

It cleans up a non-uniformity in the arguments passed to the reduction function
that has always bothered me: If there is no initial value, the first call to the
reduction function receives two sequence elements; successive calls receive an
accumulated value and a sequence element. The order of these arguments depends
on :FROM-END as well. I've taken to providing an :INITIAL-VALUE argument just to
avoid cluttering up the reduction function with the hair to handle all this.

In fact, because of this nonsense, the example given in the proposal only works
for sequences with exactly two elements. A more correct example is

  (reduce #'(lambda (x y) (+ x (person-age y))) astros :initial-value 0)

NB: Since there's an initial value you can dispense with the &optional, and the
first argument to the reduction function is now always the accumulated value.

Even with this new example, I think I'd still rather write

  (reduce #'+ astros :key #'person-age)

The intent of the code seems clearer to me here.

There are a few details missing from the proposal.

1) The key-fn is never applied to the initial value, if any.

2) The wording in CLtL about how the initial value participates in the reduction
will have to be changed a bit.

3) If the specified subsequence contains exactly one element, no initial value
is given, and a key-fn has been provided, it is applied to that element and the
resulting value is returned.

If people are unhappy about using the :KEY keyword for this purpose because of
the aesthetic problems, I'm happy to entertain suggestions for another keyword
to use instead.

		-- Andy. --