Re: Take n largest items
 From: tar@xxxxxxxxxxxxx (Thomas A. Russ)
 Date: 26 Aug 2009 17:08:26 0700
Slobodan Blazeski <slobodan.blazeski@xxxxxxxxx> writes:
I need to take the n largest items from a list in order of
appearance. Something like:
(defvar *foo* '((a 11) (b 2) (c 7) (d 2) (e 0) (f 33)))
(take 3 :key #'second)
((a 11) (b 2) (f 33))
Corrected to (take 3 *foo* :key #'second)
((a 11) (c 7) (f 33)).
OK, here's another solution to throw into the mix.
(defun take (n list &key (key #'identity))
(if (< n (length list))
(let* ((sortedlist (sort (copyseq list) #'> :key key))
(topn (subseq sortedlist 0 n)))
(removeifnot #'(lambda (x) (find x topn)) list))
list))
BTW, I doubt that it is an optimal solution.

Thomas A. Russ, USC/Information Sciences Institute
.
 FollowUps:
 Re: Take n largest items
 From: Slobodan Blazeski
 Re: Take n largest items
 References:
 Take n largest items
 From: Slobodan Blazeski
 Take n largest items
 Prev by Date: Re: Better mehod to generate a sequence of range?
 Next by Date: Re: Take n largest items
 Previous by thread: Re: Take n largest items
 Next by thread: Re: Take n largest items
 Index(es):
Relevant Pages
