[Top][All Lists]

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

sort needs a #:key argument

From: Andy Wingo
Subject: sort needs a #:key argument
Date: Sun, 18 Oct 2009 22:13:02 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.92 (gnu/linux)


Do you read guile-devel and are looking for a manageable thing to hack?
Well how about this. `sort!' needs a #:key argument.

I believe you will find more precise specifications in the Common Lisp
specifications, but the basic idea is that the comparison function of
`sort' operates not on elements of the list, but on the result of
applying a function to those elements.

Like this:

  (sort '(1 -2 3 -4) <)
     => (-4 -2 1 3)
  (sort '(1 -2 3 -4) < #:key (lambda (x) (* x x)))
     => (1 -2 3 -4)
  (sort '((a . 1) (b . -2) (c . 3)) < cdr)
     => ((b . -2) (a . 1) (c . 3))

I would do it by exposing scm_sort to Scheme as primitive-sort, and
defining a `sort' in scheme using (ice-9 optargs) and primitive-sort,
customizing the less? procedure if a #:key is given.

Then we'd need to update the documentation, and add a news entry. Any
takers? :)


reply via email to

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