add invert-comparison word

db4
Doug Coleman 2008-04-27 23:23:51 -05:00
parent 0d8ddd2a3c
commit 09c21f077b
3 changed files with 11 additions and 0 deletions

View File

@ -254,6 +254,7 @@ $nl
"Some types of objects also have an intrinsic order allowing sorting using " { $link natural-sort } ":"
{ $subsection <=> }
{ $subsection compare }
{ $subsection invert-comparison }
"Utilities for comparing objects:"
{ $subsection after? }
{ $subsection before? }

View File

@ -25,6 +25,13 @@ HELP: +eq+
HELP: +gt+
{ $description "Returned by " { $link <=> } " when the first object is strictly greater than the second object." } ;
HELP: invert-comparison
{ $values { "symbol" "a comparison symbol, +lt+, +eq+, or +gt+" }
{ "new-symbol" "a comparison symbol, +lt+, +eq+, or +gt+" } }
{ $description "Invert the comparison symbol returned by " { $link <=> } ". The output for the symbol " { $snippet "+eq+" } " is itself." }
{ $examples
{ $example "USING: math.order prettyprint ;" "+lt+ invert-comparison ." "+gt+" } } ;
HELP: compare
{ $values { "obj1" object } { "obj2" object } { "quot" "a quotation with stack effect " { $snippet "( obj -- newobj )" } } { "symbol" "a comparison symbol, +lt+, +eq+, or +gt+" } }
{ $description "Compares the results of applying the quotation to both objects via " { $link <=> } "." }

View File

@ -11,6 +11,9 @@ GENERIC: <=> ( obj1 obj2 -- n )
: (<=>) - dup 0 < [ drop +lt+ ] [ zero? +eq+ +gt+ ? ] if ;
: invert-comparison ( symbol -- new-symbol )
dup +lt+ eq? [ drop +gt+ ] [ +eq+ eq? +eq+ +lt+ ? ] if ;
M: real <=> (<=>) ;
M: integer <=> (<=>) ;