[pro] why :key arguments?

Tamas Papp tkpapp at gmail.com
Mon Jul 4 10:59:36 UTC 2011


On Mon, 04 Jul 2011 12:12:33 +0200, Svante Carl v. Erichsen wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Am 04.07.2011 11:31, schrieb Tamas Papp:
>> Why do some CL library functions have :key arguments?
>> 
>> I am asking because I am working on some statistics functions, and the
>> design choice came up.  Specifically, I can write functions like
>> 
>> (defun quantiles (sequence quantiles &key (key #'identity))
>>    ...)
>> 
>> but it is a bit cumbersome.  I can make my code simpler by relying on
>> calls like
>> 
>> (quantiles (map 'vector key vector) quantiles)
>> 
>> but this conses a bit more.
> 
> Doesn't quantiles just pass the key to sort?  Is there some smarter
> algorithm I am not aware of right now?

Eg

@inproceedings{greenwald2001space,
  title={Space-efficient online computation of quantile summaries},
  author={Greenwald, M. and Khanna, S.},
  booktitle={ACM SIGMOD Record},
  volume={30},
  number={2},
  pages={58--66},
  year={2001},
  organization={ACM}
}

I am making quantiles a generic function: it should work on objects 
returned by the method above, and also on vectors.  My problem was that

(quantiles quantile-summary #(0.25 0.5 0.75) :key something)

makes little sense, since the summary is already accumulated.  So I will 
drop key for the moment, and will probably use compiler macros.

I also thought of a lazy solution that would just save the function and 
the object, and traverse once, eg

(quantiles (with-key function object) ...)

but I need to think about that.

> Also, passing the key lets you return the complete objects (or
> whatever), not just the keys as in the map call.  For example, compare

That's a good point, I didn't think of that.

Best,

Tamas





More information about the pro mailing list