guile-devel
[Top][All Lists]
Advanced

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

Re: sort needs a #:key argument


From: Andy Wingo
Subject: Re: sort needs a #:key argument
Date: Tue, 20 Oct 2009 21:26:06 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.92 (gnu/linux)

Hi,

On Tue 20 Oct 2009 10:38, address@hidden (Ludovic Courtès) writes:

> Andy Wingo <address@hidden> writes:
>
>>   (sort '((a . 1) (b . -2) (c . 3)) < cdr)
>>      => ((b . -2) (a . 1) (c . 3))
>
> FWIW I find:
>
>   (sort '((a . 1) (b . -2) (c . 3))
>         (lambda (x y)
>           (< (cdr x) (cdr y))))
>
> more in the spirit of not “piling feature on top of feature”.

(sort l < #:key cdr) is a bit contrived. But if your key takes time to
compute, you need to use the decorate-sort-undecorate idiom, otherwise
you compute the sort key O(n log n) times instead of O(n) times. It's
part of our old Lisp tradition :)

Plus.. it's for occupational safety. Less typing for the win! :)

Andy
-- 
http://wingolog.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]