]> gitweb.factorcode.org Git - factor.git/blob - basis/sorting/slots/slots-docs.factor
Merge branch 'master' into experimental
[factor.git] / basis / sorting / slots / slots-docs.factor
1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: help.markup help.syntax kernel quotations math.order
4 sequences ;
5 IN: sorting.slots
6
7 HELP: compare-slots
8 { $values
9      { "sort-specs" "a sequence of accessors ending with a comparator" }
10      { "<=>" { $link +lt+ } " " { $link +eq+ } " or " { $link +gt+ } }
11 }
12 { $description "Compares two objects using a chain of intrinsic linear orders such that if two objects are " { $link +eq+ } ", then the next comparator is tried. The comparators are slot-name/comparator pairs." } ;
13
14 HELP: sort-by-slots
15 { $values
16      { "seq" sequence } { "sort-specs" "a sequence of accessors ending with a comparator" }
17      { "seq'" sequence }
18 }
19 { $description "Sorts a sequence of tuples by the sort-specs in " { $snippet "sort-spec" } ". A sort-spec is a sequence of slot accessors ending in a comparator." }
20 { $examples
21     "Sort by slot c, then b descending:"
22     { $example
23         "USING: accessors math.order prettyprint sorting.slots ;"
24         "IN: scratchpad"
25         "TUPLE: sort-me a b ;"
26         "{"
27         "    T{ sort-me f 2 3 } T{ sort-me f 3 2 }"
28         "    T{ sort-me f 4 3 } T{ sort-me f 2 1 }"
29         "}"
30         "{ { a>> <=> } { b>> >=< } } sort-by-slots ."
31         "{\n    T{ sort-me { a 2 } { b 3 } }\n    T{ sort-me { a 2 } { b 1 } }\n    T{ sort-me { a 3 } { b 2 } }\n    T{ sort-me { a 4 } { b 3 } }\n}"
32     }
33 } ;
34
35 HELP: split-by-slots
36 { $values
37      { "accessor-seqs" "a sequence of sequences of tuple accessors" }
38      { "quot" quotation }
39 }
40 { $description "Splits a sequence of tuples into a sequence of slices of tuples that have the same values in all slots in the accessor sequence. This word is only useful for splitting a sorted sequence, but is more efficient than partitioning in such a case." } ;
41
42 ARTICLE: "sorting.slots" "Sorting by slots"
43 "The " { $vocab-link "sorting.slots" } " vocabulary can sort tuples by slot in ascending or descending order, using subsequent slots as tie-breakers." $nl
44 "Comparing two objects by a sequence of slots:"
45 { $subsection compare-slots }
46 "Sorting a sequence by a sequence of slots:"
47 { $subsection sort-by-slots } ;
48
49 ABOUT: "sorting.slots"