aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
Diffstat (limited to 'doc')
-rw-r--r--doc/lispref/sequences.texi40
1 files changed, 4 insertions, 36 deletions
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi
index 8b4ce539669..8b06874e73c 100644
--- a/doc/lispref/sequences.texi
+++ b/doc/lispref/sequences.texi
@@ -440,6 +440,10 @@ where @var{predicate} is the @code{:lessp} argument. When using this
440form, sorting is always done in-place. 440form, sorting is always done in-place.
441@end defun 441@end defun
442 442
443@xref{Sorting}, for more functions that perform sorting. See
444@code{documentation} in @ref{Accessing Documentation}, for a useful
445example of @code{sort}.
446
443@cindex comparing values 447@cindex comparing values
444@cindex standard sorting order 448@cindex standard sorting order
445@anchor{definition of value<} 449@anchor{definition of value<}
@@ -476,42 +480,6 @@ Examples:
476@end example 480@end example
477@end defun 481@end defun
478 482
479Sometimes, computation of sort keys of list or vector elements is
480expensive, and therefore it is important to perform it the minimum
481number of times. By contrast, computing the sort keys of elements
482inside the @var{predicate} function passed to @code{sort} will generally
483perform this computation each time @var{predicate} is called with some
484element. If you can separate the computation of the sort key of an
485element into a function of its own, you can use the following sorting
486function, which guarantees that the key will be computed for each list
487or vector element exactly once.
488
489@cindex decorate-sort-undecorate
490@cindex Schwartzian transform
491@defun sort-on sequence predicate accessor
492This function stably sorts @var{sequence}, which can be a list, a
493vector, a bool-vector, or a string. It sorts by comparing the sort
494keys of the elements using @var{predicate}. The comparison function
495@var{predicate} accepts two arguments, the sort keys to compare, and
496should return non-@code{nil} if the element corresponding to the first
497key should sort before the element corresponding to the second key. The
498function computes a sort key of each element by calling the
499@var{accessor} function on that element; it does so exactly once for
500each element of @var{sequence}. The @var{accessor} function is called
501with a single argument, an element of @var{sequence}.
502
503This function implements what is known as @dfn{decorate-sort-undecorate}
504paradigm, or the Schwartzian transform. It basically trades CPU for
505memory, creating a temporary list with the computed sort keys, then
506mapping @code{car} over the result of sorting that temporary list.
507Unlike with @code{sort}, the return value is always a new list; the
508original @var{sequence} is left intact.
509@end defun
510
511@xref{Sorting}, for more functions that perform sorting. See
512@code{documentation} in @ref{Accessing Documentation}, for a useful
513example of @code{sort}.
514
515@cindex sequence functions in seq 483@cindex sequence functions in seq
516@cindex seq library 484@cindex seq library
517@cindex sequences, generalized 485@cindex sequences, generalized