[pro] why :key arguments?
Svante Carl v. Erichsen
Svante.v.Erichsen at web.de
Mon Jul 4 10:12:33 UTC 2011
-----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?
Also, passing the key lets you return the complete objects (or
whatever), not just the keys as in the map call. For example, compare
(quantiles #((a 1) (b 4) (c 2) (d 2) (e 5)) 2 :key #'second)
with
(quantiles (map 'vector #'second
#((a 1) (b 4) (c 2) (d 2) (e 5)))
2)
In the first case, you could return (d 2), but not in the second.
Maybe I'm just missing the point. :)
Best wishes,
Svante
- --
Svante Carl v. Erichsen
Wentorfer Str. 96
21029 Hamburg
+49-(0)40-34923721
+49-(0)160-6941474
Svante.v.Erichsen at web.de
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk4RkhEACgkQFZBb0npmKszyrwCfXpVU9rNlejtD6MXkyPZLIDp6
MBcAn0BpfaEUZgfTtXB4dm3vy86wH1/t
=YRm+
-----END PGP SIGNATURE-----
More information about the pro
mailing list