rename human-<=> to human<=>, same for human>=<
parent
fad7520323
commit
c2df6db543
|
@ -11,19 +11,19 @@ HELP: find-numbers
|
||||||
}
|
}
|
||||||
{ $description "Splits a string on numbers and returns a sequence of sequences and integers." } ;
|
{ $description "Splits a string on numbers and returns a sequence of sequences and integers." } ;
|
||||||
|
|
||||||
HELP: human-<=>
|
HELP: human<=>
|
||||||
{ $values
|
{ $values
|
||||||
{ "obj1" object } { "obj2" object }
|
{ "obj1" object } { "obj2" object }
|
||||||
{ "<=>" "an ordering specifier" }
|
{ "<=>" "an ordering specifier" }
|
||||||
}
|
}
|
||||||
{ $description "Compares two objects after converting numbers in the string into integers." } ;
|
{ $description "Compares two objects after converting numbers in the string into integers." } ;
|
||||||
|
|
||||||
HELP: human->=<
|
HELP: human>=<
|
||||||
{ $values
|
{ $values
|
||||||
{ "obj1" object } { "obj2" object }
|
{ "obj1" object } { "obj2" object }
|
||||||
{ ">=<" "an ordering specifier" }
|
{ ">=<" "an ordering specifier" }
|
||||||
}
|
}
|
||||||
{ $description "Compares two objects using the " { $link human-<=> } " word and inverts the result." } ;
|
{ $description "Compares two objects using the " { $link human<=> } " word and inverts the result." } ;
|
||||||
|
|
||||||
HELP: human-compare
|
HELP: human-compare
|
||||||
{ $values
|
{ $values
|
||||||
|
@ -44,22 +44,22 @@ HELP: human-sort-keys
|
||||||
{ "seq" "an alist" }
|
{ "seq" "an alist" }
|
||||||
{ "sortedseq" "a new sorted sequence" }
|
{ "sortedseq" "a new sorted sequence" }
|
||||||
}
|
}
|
||||||
{ $description "Sorts the elements comparing first elements of pairs using the " { $link human-<=> } " word." } ;
|
{ $description "Sorts the elements comparing first elements of pairs using the " { $link human<=> } " word." } ;
|
||||||
|
|
||||||
HELP: human-sort-values
|
HELP: human-sort-values
|
||||||
{ $values
|
{ $values
|
||||||
{ "seq" "an alist" }
|
{ "seq" "an alist" }
|
||||||
{ "sortedseq" "a new sorted sequence" }
|
{ "sortedseq" "a new sorted sequence" }
|
||||||
}
|
}
|
||||||
{ $description "Sorts the elements comparing second elements of pairs using the " { $link human-<=> } " word." } ;
|
{ $description "Sorts the elements comparing second elements of pairs using the " { $link human<=> } " word." } ;
|
||||||
|
|
||||||
{ <=> >=< human-compare human-sort human-sort-keys human-sort-values } related-words
|
{ <=> >=< human-compare human-sort human-sort-keys human-sort-values } related-words
|
||||||
|
|
||||||
ARTICLE: "sorting.human" "sorting.human"
|
ARTICLE: "sorting.human" "sorting.human"
|
||||||
"The " { $vocab-link "sorting.human" } " vocabulary sorts by numbers as a human would -- by comparing their magnitudes -- rather than in a lexicographic way. For example, sorting a1, a10, a03, a2 with human sort returns a1, a2, a03, a10, while sorting with natural sort returns a03, a1, a10, a2." $nl
|
"The " { $vocab-link "sorting.human" } " vocabulary sorts by numbers as a human would -- by comparing their magnitudes -- rather than in a lexicographic way. For example, sorting a1, a10, a03, a2 with human sort returns a1, a2, a03, a10, while sorting with natural sort returns a03, a1, a10, a2." $nl
|
||||||
"Comparing two objects:"
|
"Comparing two objects:"
|
||||||
{ $subsection human-<=> }
|
{ $subsection human<=> }
|
||||||
{ $subsection human->=< }
|
{ $subsection human>=< }
|
||||||
{ $subsection human-compare }
|
{ $subsection human-compare }
|
||||||
"Sort a sequence:"
|
"Sort a sequence:"
|
||||||
{ $subsection human-sort }
|
{ $subsection human-sort }
|
||||||
|
|
|
@ -7,13 +7,13 @@ 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<=> ( obj1 obj2 -- <=> ) [ find-numbers ] bi@ <=> ;
|
||||||
|
|
||||||
: human->=< ( obj1 obj2 -- >=< ) human-<=> invert-comparison ; inline
|
: human>=< ( obj1 obj2 -- >=< ) human<=> invert-comparison ; inline
|
||||||
|
|
||||||
: human-compare ( obj1 obj2 quot -- <=> ) bi@ human-<=> ;
|
: human-compare ( obj1 obj2 quot -- <=> ) bi@ human<=> ;
|
||||||
|
|
||||||
: human-sort ( seq -- seq' ) [ human-<=> ] sort ;
|
: human-sort ( seq -- seq' ) [ human<=> ] sort ;
|
||||||
|
|
||||||
: human-sort-keys ( seq -- sortedseq )
|
: human-sort-keys ( seq -- sortedseq )
|
||||||
[ [ first ] human-compare ] sort ;
|
[ [ first ] human-compare ] sort ;
|
||||||
|
|
|
@ -41,7 +41,7 @@ TUPLE: tuple2 d ;
|
||||||
T{ sort-test f 1 1 11 }
|
T{ sort-test f 1 1 11 }
|
||||||
T{ sort-test f 2 5 3 }
|
T{ sort-test f 2 5 3 }
|
||||||
T{ sort-test f 2 5 2 }
|
T{ sort-test f 2 5 2 }
|
||||||
} { { a>> human-<=> } { b>> human->=< } { c>> <=> } } sort-by-slots
|
} { { a>> human<=> } { b>> human>=< } { c>> <=> } } sort-by-slots
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
[
|
[
|
||||||
|
@ -64,7 +64,7 @@ TUPLE: tuple2 d ;
|
||||||
T{ sort-test f 2 5 3 }
|
T{ sort-test f 2 5 3 }
|
||||||
T{ sort-test f 2 5 2 }
|
T{ sort-test f 2 5 2 }
|
||||||
}
|
}
|
||||||
{ { a>> human-<=> } { b>> <=> } } [ sort-by-slots ] keep
|
{ { a>> human<=> } { b>> <=> } } [ sort-by-slots ] keep
|
||||||
[ but-last-slice ] map split-by-slots [ >array ] map
|
[ but-last-slice ] map split-by-slots [ >array ] map
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue