]> gitweb.factorcode.org Git - factor.git/commitdiff
sorting: change sort-keys and sort-values to generics.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 7 Apr 2011 15:57:26 +0000 (08:57 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 7 Apr 2011 15:57:26 +0000 (08:57 -0700)
core/sorting/sorting-docs.factor
core/sorting/sorting.factor

index 5b013f95fb76735418b968a4ce491697681fd1b9..4877cdf4100df4ffa855c8b49a19dbe360fbe965 100644 (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." } ;
 
 HELP: sort-keys
-{ $values { "seq" "an alist" } { "sortedseq" "a new sorted sequence" } }
-{ $description "Sorts the elements of " { $snippet "seq" } " comparing first elements of pairs using the " { $link <=> } " word." } ;
+{ $values { "obj" "an object" } { "sortedseq" "a new sorted sequence" } }
+{ $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
-{ $values { "seq" "an alist" } { "sortedseq" "a new sorted sequence" } }
-{ $description "Sorts the elements of " { $snippet "seq" } " comparing second elements of pairs using the " { $link <=> } " word." } ;
+{ $values { "obj" "an object" } { "sortedseq" "a new sorted sequence" } }
+{ $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
 { $values { "seq" "a sequence of real numbers" } { "sortedseq" "a new sorted sequence" } }
index b8258b239bfebd28e1d126d22541262de9374a2e..b26a34b41ee677cf358499dd95ee61659168ec81 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright (C) 2005, 2009 Slava Pestov.
 ! 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 ;
 IN: sorting
 
@@ -160,8 +160,16 @@ PRIVATE>
 : inv-sort-with ( seq quot -- sortedseq )
     [ 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 ;