sorting: change sort-keys and sort-values to generics.

db4
John Benediktsson 2011-04-07 08:57:26 -07:00
parent 392494733b
commit 9efa64831f
2 changed files with 15 additions and 7 deletions

View File

@ -36,12 +36,12 @@ HELP: inv-sort-with
{ $description "Sorts the elements of " { $snippet "seq" } " by applying " { $link compare } " with " { $snippet "quot" } " to each pair of elements in the sequence and inverting the results." } ; { $description "Sorts the elements of " { $snippet "seq" } " by applying " { $link compare } " with " { $snippet "quot" } " to each pair of elements in the sequence and inverting the results." } ;
HELP: sort-keys HELP: sort-keys
{ $values { "seq" "an alist" } { "sortedseq" "a new sorted sequence" } } { $values { "obj" "an object" } { "sortedseq" "a new sorted sequence" } }
{ $description "Sorts the elements of " { $snippet "seq" } " comparing first elements of pairs using the " { $link <=> } " word." } ; { $description "Sorts the elements of " { $snippet "obj" } " (converting to an alist first if not a sequence), comparing first elements of pairs using the " { $link <=> } " word." } ;
HELP: sort-values HELP: sort-values
{ $values { "seq" "an alist" } { "sortedseq" "a new sorted sequence" } } { $values { "obj" "an object" } { "sortedseq" "a new sorted sequence" } }
{ $description "Sorts the elements of " { $snippet "seq" } " comparing second elements of pairs using the " { $link <=> } " word." } ; { $description "Sorts the elements of " { $snippet "obj" } " (converting to an alist first if not a sequence), comparing second elements of pairs using the " { $link <=> } " word." } ;
HELP: natural-sort HELP: natural-sort
{ $values { "seq" "a sequence of real numbers" } { "sortedseq" "a new sorted sequence" } } { $values { "seq" "a sequence of real numbers" } { "sortedseq" "a new sorted sequence" } }

View File

@ -1,6 +1,6 @@
! Copyright (C) 2005, 2009 Slava Pestov. ! Copyright (C) 2005, 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays kernel math vectors math.order USING: accessors arrays assocs kernel math vectors math.order
sequences sequences.private ; sequences sequences.private ;
IN: sorting IN: sorting
@ -160,8 +160,16 @@ PRIVATE>
: inv-sort-with ( seq quot -- sortedseq ) : inv-sort-with ( seq quot -- sortedseq )
[ compare invert-comparison ] curry sort ; inline [ compare invert-comparison ] curry sort ; inline
: sort-keys ( seq -- sortedseq ) [ first ] sort-with ; GENERIC: sort-keys ( obj -- sortedseq )
: sort-values ( seq -- sortedseq ) [ second ] sort-with ; M: object sort-keys >alist sort-keys ;
M: sequence sort-keys [ first ] sort-with ;
GENERIC: sort-values ( obj -- sortedseq )
M: object sort-values >alist sort-values ;
M: sequence sort-values [ second ] sort-with ;
: sort-pair ( a b -- c d ) 2dup after? [ swap ] when ; : sort-pair ( a b -- c d ) 2dup after? [ swap ] when ;