Documentation updates
parent
1bb8fe41e8
commit
5a3581acbc
|
@ -3,9 +3,9 @@ math.private words ;
|
|||
IN: math.order
|
||||
|
||||
HELP: <=>
|
||||
{ $values { "obj1" object } { "obj2" object } { "symbol" symbol } }
|
||||
{ $values { "obj1" object } { "obj2" object } { "<=>" "an ordering specifier" } }
|
||||
{ $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 linear order, for example, the natural order for real numbers and lexicographic order for strings."
|
||||
$nl
|
||||
"The output value is one of the following:"
|
||||
{ $list
|
||||
|
@ -16,23 +16,23 @@ HELP: <=>
|
|||
} ;
|
||||
|
||||
HELP: +lt+
|
||||
{ $description "Returned by " { $link <=> } " when the first object is strictly less than the second object." } ;
|
||||
{ $description "Output by " { $link <=> } " when the first object is strictly less than the second object." } ;
|
||||
|
||||
HELP: +eq+
|
||||
{ $description "Returned by " { $link <=> } " when the first object is equal to the second object." } ;
|
||||
{ $description "Output by " { $link <=> } " when the first object is equal to the second object." } ;
|
||||
|
||||
HELP: +gt+
|
||||
{ $description "Returned by " { $link <=> } " when the first object is strictly greater than the second object." } ;
|
||||
{ $description "Output by " { $link <=> } " when the first object is strictly greater than the second object." } ;
|
||||
|
||||
HELP: invert-comparison
|
||||
{ $values { "symbol" symbol }
|
||||
{ "new-symbol" symbol } }
|
||||
{ $description "Invert the comparison symbol returned by " { $link <=> } ". The output for the symbol " { $snippet "+eq+" } " is itself." }
|
||||
{ $values { "<=>" symbol }
|
||||
{ "<=>'" symbol } }
|
||||
{ $description "Invert the comparison symbol returned by " { $link <=> } "." }
|
||||
{ $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+" } }
|
||||
{ $values { "obj1" object } { "obj2" object } { "quot" "a quotation with stack effect " { $snippet "( obj -- newobj )" } } { "<=>" "an ordering specifier" } }
|
||||
{ $description "Compares the results of applying the quotation to both objects via " { $link <=> } "." }
|
||||
{ $examples { $example "USING: kernel math.order prettyprint sequences ;" "\"hello\" \"hi\" [ length ] compare ." "+gt+" }
|
||||
} ;
|
||||
|
@ -76,19 +76,24 @@ HELP: [-]
|
|||
{ $values { "x" real } { "y" real } { "z" real } }
|
||||
{ $description "Subtracts " { $snippet "y" } " from " { $snippet "x" } ". If the result is less than zero, outputs zero." } ;
|
||||
|
||||
ARTICLE: "math.order" "Ordered objects"
|
||||
ARTICLE: "order-specifiers" "Ordering specifiers"
|
||||
"Ordering words such as " { $link <=> } " output one of the following values, indicating that of two objects being compared, the first is less than the second, the two are equal, or that the first is greater than the second:"
|
||||
{ $subsection +lt+ }
|
||||
{ $subsection +eq+ }
|
||||
{ $subsection +gt+ } ;
|
||||
|
||||
ARTICLE: "math.order" "Linear order protocol"
|
||||
"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+ }
|
||||
"The above words output order specifiers."
|
||||
{ $subsection "order-specifiers" }
|
||||
"Utilities for comparing objects:"
|
||||
{ $subsection after? }
|
||||
{ $subsection before? }
|
||||
{ $subsection after=? }
|
||||
{ $subsection before=? } ;
|
||||
{ $subsection before=? }
|
||||
{ $see-also "sequences-sorting" } ;
|
||||
|
||||
ABOUT: "math.order"
|
||||
|
|
|
@ -7,11 +7,11 @@ SYMBOL: +lt+
|
|||
SYMBOL: +eq+
|
||||
SYMBOL: +gt+
|
||||
|
||||
: invert-comparison ( symbol -- new-symbol )
|
||||
: invert-comparison ( <=> -- <=>' )
|
||||
#! Can't use case, index or nth here
|
||||
dup +lt+ eq? [ drop +gt+ ] [ +eq+ eq? +eq+ +lt+ ? ] if ;
|
||||
|
||||
GENERIC: <=> ( obj1 obj2 -- symbol )
|
||||
GENERIC: <=> ( obj1 obj2 -- <=> )
|
||||
|
||||
M: real <=> 2dup < [ 2drop +lt+ ] [ number= +eq+ +gt+ ? ] if ;
|
||||
|
||||
|
@ -38,4 +38,4 @@ M: real after=? ( obj1 obj2 -- ? ) >= ;
|
|||
|
||||
: [-] ( x y -- z ) - 0 max ; inline
|
||||
|
||||
: compare ( obj1 obj2 quot -- symbol ) bi@ <=> ; inline
|
||||
: compare ( obj1 obj2 quot -- <=> ) bi@ <=> ; inline
|
||||
|
|
|
@ -3,12 +3,8 @@ sequences math.order ;
|
|||
IN: sorting
|
||||
|
||||
ARTICLE: "sequences-sorting" "Sorting and binary search"
|
||||
"Sorting and binary search combinators all take comparator quotations with stack effect " { $snippet "( elt1 elt2 -- n )" } " that order the two given elements and output a value whose sign denotes the result:"
|
||||
{ $list
|
||||
{ "positive - indicates that " { $snippet "elt1" } " follows " { $snippet "elt2" } }
|
||||
{ "zero - indicates that " { $snippet "elt1" } " is ordered equivalently to " { $snippet "elt2" } }
|
||||
{ "negative - indicates that " { $snippet "elt1" } " precedes " { $snippet "elt2" } }
|
||||
}
|
||||
"Sorting and binary search combinators all take comparator quotations with stack effect " { $snippet "( elt1 elt2 -- <=> )" } ", where the output value is one of the three " { $link "order-specifiers" } "."
|
||||
$nl
|
||||
"Sorting a sequence with a custom comparator:"
|
||||
{ $subsection sort }
|
||||
"Sorting a sequence with common comparators:"
|
||||
|
@ -19,8 +15,10 @@ ARTICLE: "sequences-sorting" "Sorting and binary search"
|
|||
{ $subsection binsearch }
|
||||
{ $subsection binsearch* } ;
|
||||
|
||||
ABOUT: "sequences-sorting"
|
||||
|
||||
HELP: sort
|
||||
{ $values { "seq" "a sequence" } { "quot" "a comparator quotation" } { "sortedseq" "a new sorted sequence" } }
|
||||
{ $values { "seq" "a sequence" } { "quot" "a quotation with stack effect " { $snippet "( obj1 obj2 -- <=> )" } } { "sortedseq" "a new sorted sequence" } }
|
||||
{ $description "Sorts the elements into a new sequence of the same class as " { $snippet "seq" } "." } ;
|
||||
|
||||
HELP: sort-keys
|
||||
|
@ -52,13 +50,13 @@ HELP: partition
|
|||
{ $description "Outputs a slice of the first or second half of the sequence, respectively, depending on the integer's sign." } ;
|
||||
|
||||
HELP: binsearch
|
||||
{ $values { "elt" object } { "seq" "a sorted sequence" } { "quot" "a comparator quotation" } { "i" "the index of the search result" } }
|
||||
{ $values { "elt" object } { "seq" "a sorted sequence" } { "quot" "a quotation with stack effect " { $snippet "( obj1 obj2 -- <=> )" } } { "i" "the index of the search result" } }
|
||||
{ $description "Given a sequence that is sorted with respect to the " { $snippet "quot" } " comparator, searches for an element equal to " { $snippet "elt" } ", or failing that, the greatest element smaller than " { $snippet "elt" } ". Comparison is performed with " { $snippet "quot" } "."
|
||||
$nl
|
||||
"Outputs f if the sequence is empty. If the sequence has at least one element, this word always outputs a valid index." } ;
|
||||
|
||||
HELP: binsearch*
|
||||
{ $values { "elt" object } { "seq" "a sorted sequence" } { "quot" "a comparator quotation" } { "result" "the search result" } }
|
||||
{ $values { "elt" object } { "seq" "a sorted sequence" } { "quot" "a quotation with stack effect " { $snippet "( obj1 obj2 -- <=> )" } } { "result" "the search result" } }
|
||||
{ $description "Variant of " { $link binsearch } " which outputs the found element rather than its index in the sequence."
|
||||
$nl
|
||||
"Outputs " { $link f } " if the sequence is empty. If the sequence has at least one element, this word always outputs a sequence element." } ;
|
||||
|
|
Loading…
Reference in New Issue