diff man/lispref/lists.texi @ 5182:2e528066e2fc

Move #'sort*, #'fill, #'merge to C from cl-seq.el. lisp/ChangeLog addition: 2010-04-01 Aidan Kehoe <kehoea@parhasard.net> * cl-seq.el (fill, sort*, merge): Move these functions to fns.c. (stable-sort): Make this docstring reflect the argument names used in the #'sort* docstring. * cl-macs.el (stable-sort): Make #'stable-sort exactly equivalent to #'sort* in compiled code. * bytecomp.el (byte-compile-maybe-add-*): New macro, for functions like #'sort and #'mapcar that, to be strictly compatible, should only take two args, but in our implementation can take more, because they're aliases of #'sort* and #'mapcar*. (byte-compile-mapcar, byte-compile-sort, byte-compile-fillarray): Use this new macro. (map-into): Add a byte-compile method for #'map-into in passing. * apropos.el (apropos-print): Use #'sort* with a :key argument, now it's in C. * compat.el (extent-at): Ditto. * register.el (list-registers): Ditto. * package-ui.el (pui-list-packages): Ditto. * help.el (sorted-key-descriptions): Ditto. src/ChangeLog addition: 2010-03-31 Aidan Kehoe <kehoea@parhasard.net> * fns.c (STRING_DATA_TO_OBJECT_ARRAY) (BIT_VECTOR_TO_OBJECT_ARRAY, c_merge_predicate_key) (c_merge_predicate_nokey, list_merge, array_merge) (list_array_merge_into_list, list_list_merge_into_array) (list_array_merge_into_array, CHECK_KEY_ARGUMENT, Fmerge) (list_sort, array_sort, FsortX): Move #'sort*, #'fill, #'merge from cl-seq.el to C, extending the implementations of Fsort, Ffillarray, and merge() to do so. * keymap.c (keymap_submaps, map_keymap_sort_predicate) (describe_map_sort_predicate): Change the calling semantics of the C sort predicates to return a non-nil Lisp object if the first argument is less than the second, rather than C integers. * fontcolor-msw.c (sort_font_list_function): * fileio.c (build_annotations): * dired.c (Fdirectory_files): * abbrev.c (Finsert_abbrev_table_description): Call list_sort instead of Fsort, list_merge instead of merge() in these functions. man/ChangeLog addition: 2010-04-01 Aidan Kehoe <kehoea@parhasard.net> * lispref/lists.texi (Rearrangement): Update the documentation of #'sort here, now that it accepts any type of sequence and the KEY keyword argument. (Though this is probably now the wrong place for this function, given that.)
author Aidan Kehoe <kehoea@parhasard.net>
date Thu, 01 Apr 2010 20:22:50 +0100
parents b880e7bdec21
children 9f738305f80f
line wrap: on
line diff
--- a/man/lispref/lists.texi	Mon Mar 29 23:23:33 2010 -0500
+++ b/man/lispref/lists.texi	Thu Apr 01 20:22:50 2010 +0100
@@ -1050,31 +1050,54 @@
 @end smallexample
 @end defun
 
-@defun sort list predicate
+@defun sort* sequence predicate &key (key #'identity)
 @cindex stable sort
 @cindex sorting lists
-This function sorts @var{list} stably, though destructively, and
-returns the sorted list.  It compares elements using @var{predicate}.  A
+@cindex sorting arrays
+@cindex sort
+This function sorts @var{sequence} stably, though destructively, and
+returns the sorted sequence.  It compares elements using @var{predicate}.  A
 stable sort is one in which elements with equal sort keys maintain their
 relative order before and after the sort.  Stability is important when
 successive sorts are used to order elements according to different
 criteria.
 
+@var{sequence} can be any sequence, that is, a list, a vector, a
+bit-vector, or a string.  
+
 The argument @var{predicate} must be a function that accepts two
-arguments.  It is called with two elements of @var{list}.  To get an
+arguments.  It is called with two elements of @var{sequence}.  To get an
 increasing order sort, the @var{predicate} should return @code{t} if the
 first element is ``less than'' the second, or @code{nil} if not.
 
-The destructive aspect of @code{sort} is that it rearranges the cons
-cells forming @var{list} by changing @sc{cdr}s.  A nondestructive sort
+The keyword argument @var{key}, if supplied, is a function used to
+extract an object to be used for comparison from each element of
+@var{sequence}, and defaults to @code{identity}.  For example, to sort a
+vector of lists by the numeric value of the first element, you could use
+the following code:
+
+@example
+@group
+(setq example-vector [(1 "foo") (3.14159 bar) (2 . quux)])
+     @result{} [(1 "foo") (3.14159 bar) (2 . quux)]
+@end group
+@group
+(sort* example-vector #'< :key #'car)
+     @result{} [(1 "foo") (2 . quux) (3.14159 bar)]
+@end group
+@end example
+
+If @var{sequence} is a list, @code{sort*} rearranges the cons cells
+forming @var{sequence} by changing @sc{cdr}s.  A nondestructive sort
 function would create new cons cells to store the elements in their
-sorted order.  If you wish to make a sorted copy without destroying the
+sorted order.  @code{sort*} treats other sequence types in an analogous
+fashion---if you wish to make a sorted copy without destroying the
 original, copy it first with @code{copy-sequence} and then sort.
 
-Sorting does not change the @sc{car}s of the cons cells in @var{list};
-the cons cell that originally contained the element @code{a} in
-@var{list} still has @code{a} in its @sc{car} after sorting, but it now
-appears in a different position in the list due to the change of
+Sorting will not change the @sc{car}s of the cons cells of a list
+@var{sequence}; the cons cell that originally contained the element @code{a} in
+@var{sequence} still has @code{a} in its @sc{car} after sorting, but it now
+appears in a different position in the sequence due to the change of
 @sc{cdr}s.  For example:
 
 @example
@@ -1083,7 +1106,7 @@
      @result{} (1 3 2 6 5 4 0)
 @end group
 @group
-(sort nums '<)
+(sort* nums '<)
      @result{} (0 1 2 3 4 5 6)
 @end group
 @group
@@ -1096,17 +1119,23 @@
 Note that the list in @code{nums} no longer contains 0; this is the same
 cons cell that it was before, but it is no longer the first one in the
 list.  Don't assume a variable that formerly held the argument now holds
-the entire sorted list!  Instead, save the result of @code{sort} and use
+the entire sorted list!  Instead, save the result of @code{sort*} and use
 that.  Most often we store the result back into the variable that held
-the original list:
+the original sequence:
 
 @example
-(setq nums (sort nums '<))
+(setq nums (sort* nums '<))
 @end example
 
+In this implementation, @code{sort} is a function alias for
+@code{sort*}, and accepts the same arguments.  In older XEmacs, and in
+current GNU Emacs, @code{sort} only accepted lists, and did not accept
+the @var{key} argument, so the byte-compiler will warn you if you call
+@code{sort} with more than two arguments.
+
 @xref{Sorting}, for more functions that perform sorting.
 See @code{documentation} in @ref{Accessing Documentation}, for a
-useful example of @code{sort}.
+useful example of @code{sort*}.
 @end defun
 
 @node Sets And Lists