Cleanup erg's cleanup

db4
Slava Pestov 2008-04-28 14:52:03 -05:00
parent 1147b07bb9
commit 1f7be9945b
5 changed files with 29 additions and 19 deletions

View File

@ -1,5 +1,5 @@
USING: generic help.markup help.syntax math memory USING: generic help.markup help.syntax math memory
namespaces sequences kernel.private layouts sorting classes namespaces sequences kernel.private layouts classes
kernel.private vectors combinators quotations strings words kernel.private vectors combinators quotations strings words
assocs arrays math.order ; assocs arrays math.order ;
IN: kernel IN: kernel
@ -241,7 +241,7 @@ ARTICLE: "conditionals" "Conditionals and logic"
"See " { $link "combinators" } " for forms which abstract away common patterns involving multiple nested branches." "See " { $link "combinators" } " for forms which abstract away common patterns involving multiple nested branches."
{ $see-also "booleans" "bitwise-arithmetic" both? either? } ; { $see-also "booleans" "bitwise-arithmetic" both? either? } ;
ARTICLE: "equality" "Equality and comparison testing" ARTICLE: "equality" "Equality"
"There are two distinct notions of ``sameness'' when it comes to objects. You can test if two references point to the same object (" { $emphasis "identity comparison" } "), or you can test if two objects are equal in a domain-specific sense, usually by being instances of the same class, and having equal slot values (" { $emphasis "value comparison" } "). Both notions of equality are equality relations in the mathematical sense." "There are two distinct notions of ``sameness'' when it comes to objects. You can test if two references point to the same object (" { $emphasis "identity comparison" } "), or you can test if two objects are equal in a domain-specific sense, usually by being instances of the same class, and having equal slot values (" { $emphasis "value comparison" } "). Both notions of equality are equality relations in the mathematical sense."
$nl $nl
"Identity comparison:" "Identity comparison:"
@ -250,16 +250,8 @@ $nl
{ $subsection = } { $subsection = }
"Custom value comparison methods:" "Custom value comparison methods:"
{ $subsection equal? } { $subsection equal? }
"Utility class:"
{ $subsection identity-tuple } { $subsection identity-tuple }
"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? }
{ $subsection after=? }
{ $subsection before=? }
"An object can be cloned; the clone has distinct identity but equal value:" "An object can be cloned; the clone has distinct identity but equal value:"
{ $subsection clone } ; { $subsection clone } ;
@ -394,8 +386,6 @@ HELP: identity-tuple
{ $unchecked-example "T{ foo } dup clone = ." "f" } { $unchecked-example "T{ foo } dup clone = ." "f" }
} ; } ;
{ <=> compare natural-sort sort-keys sort-values } related-words
HELP: clone HELP: clone
{ $values { "obj" object } { "cloned" "a new object" } } { $values { "obj" object } { "cloned" "a new object" } }
{ $contract "Outputs a new object equal to the given object. This is not guaranteed to actually copy the object; it does nothing with immutable objects, and does not copy words either. However, sequences and tuples can be cloned to obtain a shallow copy of the original." } ; { $contract "Outputs a new object equal to the given object. This is not guaranteed to actually copy the object; it does nothing with immutable objects, and does not copy words either. However, sequences and tuples can be cloned to obtain a shallow copy of the original." } ;

View File

@ -1,9 +1,9 @@
USING: help.markup help.syntax kernel math sequences quotations USING: help.markup help.syntax kernel math quotations
math.private ; math.private words ;
IN: math.order IN: math.order
HELP: <=> HELP: <=>
{ $values { "obj1" object } { "obj2" object } { "n" real } } { $values { "obj1" object } { "obj2" object } { "symbol" symbol } }
{ $contract { $contract
"Compares two objects using an intrinsic total order, for example, the natural order for real numbers and lexicographic order for strings." "Compares two objects using an intrinsic total order, for example, the natural order for real numbers and lexicographic order for strings."
$nl $nl
@ -13,7 +13,6 @@ HELP: <=>
{ { $link +eq+ } " - indicating that " { $snippet "obj1" } " is equal to " { $snippet "obj2" } } { { $link +eq+ } " - indicating that " { $snippet "obj1" } " is equal to " { $snippet "obj2" } }
{ { $link +gt+ } " - indicating that " { $snippet "obj1" } " follows " { $snippet "obj2" } } { { $link +gt+ } " - indicating that " { $snippet "obj1" } " follows " { $snippet "obj2" } }
} }
"The default implementation treats the two objects as sequences, and recursively compares their elements. So no extra work is required to compare sequences lexicographically."
} ; } ;
HELP: +lt+ HELP: +lt+
@ -77,3 +76,19 @@ HELP: [-]
{ $values { "x" real } { "y" real } { "z" real } } { $values { "x" real } { "y" real } { "z" real } }
{ $description "Subtracts " { $snippet "y" } " from " { $snippet "x" } ". If the result is less than zero, outputs zero." } ; { $description "Subtracts " { $snippet "y" } " from " { $snippet "x" } ". If the result is less than zero, outputs zero." } ;
ARTICLE: "math.order" "Ordered objects"
"Some classes have an intrinsic order amongst instances:"
{ $subsection <=> }
{ $subsection compare }
{ $subsection invert-comparison }
"The above words return one of the following symbols:"
{ $subsection +lt+ }
{ $subsection +eq+ }
{ $subsection +gt+ }
"Utilities for comparing objects:"
{ $subsection after? }
{ $subsection before? }
{ $subsection after=? }
{ $subsection before=? } ;
ABOUT: "math.order"

View File

@ -7,11 +7,13 @@ SYMBOL: +lt+
SYMBOL: +eq+ SYMBOL: +eq+
SYMBOL: +gt+ SYMBOL: +gt+
GENERIC: <=> ( obj1 obj2 -- n ) GENERIC: <=> ( obj1 obj2 -- symbol )
: (<=>) - dup 0 < [ drop +lt+ ] [ zero? +eq+ +gt+ ? ] if ; : (<=>) ( a b -- symbol )
2dup < [ 2drop +lt+ ] [ number= +eq+ +gt+ ? ] if ; inline
: invert-comparison ( symbol -- new-symbol ) : invert-comparison ( symbol -- new-symbol )
#! Can't use case, index or nth here
dup +lt+ eq? [ drop +gt+ ] [ +eq+ eq? +eq+ +lt+ ? ] if ; dup +lt+ eq? [ drop +gt+ ] [ +eq+ eq? +eq+ +lt+ ? ] if ;
M: real <=> (<=>) ; M: real <=> (<=>) ;

View File

@ -62,3 +62,5 @@ HELP: binsearch*
{ $description "Variant of " { $link binsearch } " which outputs the found element rather than its index in the sequence." { $description "Variant of " { $link binsearch } " which outputs the found element rather than its index in the sequence."
$nl $nl
"Outputs " { $link f } " if the sequence is empty. If the sequence has at least one element, this word always outputs a sequence element." } ; "Outputs " { $link f } " if the sequence is empty. If the sequence has at least one element, this word always outputs a sequence element." } ;
{ <=> compare natural-sort sort-keys sort-values } related-words

View File

@ -104,6 +104,7 @@ $nl
ARTICLE: "objects" "Objects" ARTICLE: "objects" "Objects"
"An " { $emphasis "object" } " is any datum which may be identified. All values are objects in Factor. Each object carries type information, and types are checked at runtime; Factor is dynamically typed." "An " { $emphasis "object" } " is any datum which may be identified. All values are objects in Factor. Each object carries type information, and types are checked at runtime; Factor is dynamically typed."
{ $subsection "equality" } { $subsection "equality" }
{ $subsection "math.order" }
{ $subsection "classes" } { $subsection "classes" }
{ $subsection "tuples" } { $subsection "tuples" }
{ $subsection "generic" } { $subsection "generic" }