use functors to define human-sort

db4
Doug Coleman 2009-04-03 17:28:31 -05:00
parent a42b872a23
commit 32954b75ad
2 changed files with 3 additions and 16 deletions

View File

@ -35,7 +35,7 @@ HELP: human-compare
HELP: human-sort HELP: human-sort
{ $values { $values
{ "seq" sequence } { "seq" sequence }
{ "seq'" sequence } { "sortedseq" sequence }
} }
{ $description "Sorts a sequence of objects by comparing the magnitude of any integers in the input string using the <=> word." } ; { $description "Sorts a sequence of objects by comparing the magnitude of any integers in the input string using the <=> word." } ;

View File

@ -1,22 +1,9 @@
! Copyright (C) 2008 Doug Coleman, Slava Pestov. ! Copyright (C) 2008 Doug Coleman, Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: peg.ebnf math.parser kernel assocs sorting fry USING: math.parser peg.ebnf sorting.functor ;
math.order sequences ascii splitting.monotonic ;
IN: sorting.human IN: sorting.human
: find-numbers ( string -- seq ) : find-numbers ( string -- seq )
[EBNF Result = ([0-9]+ => [[ string>number ]] | (!([0-9]) .)+)* EBNF] ; [EBNF Result = ([0-9]+ => [[ string>number ]] | (!([0-9]) .)+)* EBNF] ;
: human<=> ( obj1 obj2 -- <=> ) [ find-numbers ] bi@ <=> ; << "human" [ find-numbers ] define-sorting >>
: human>=< ( obj1 obj2 -- >=< ) human<=> invert-comparison ; inline
: human-compare ( obj1 obj2 quot -- <=> ) bi@ human<=> ; inline
: human-sort ( seq -- seq' ) [ human<=> ] sort ;
: human-sort-keys ( seq -- sortedseq )
[ [ first ] human-compare ] sort ;
: human-sort-values ( seq -- sortedseq )
[ [ second ] human-compare ] sort ;