]> gitweb.factorcode.org Git - factor.git/blob - basis/sorting/slots/slots-docs.factor
Switch to https urls
[factor.git] / basis / sorting / slots / slots-docs.factor
1 ! Copyright (C) 2009 Doug Coleman.
2 ! See https://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   { "obj1" object }
10   { "obj2" object }
11   { "sort-specs" "a sequence of accessors ending with a comparator" }
12   { "<=>" { $link +lt+ } " " { $link +eq+ } " or " { $link +gt+ } }
13 }
14 { $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." } ;
15
16 HELP: sort-by
17 { $values
18     { "seq" sequence } { "sort-specs" "a sequence of accessors ending with a comparator" }
19     { "seq'" sequence }
20 }
21 { $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." }
22 { $examples
23     "Sort by slot a, then b descending:"
24     { $example
25         "USING: accessors math.order prettyprint sorting.slots ;"
26         "IN: scratchpad"
27         "TUPLE: sort-me a b ;"
28         "{"
29         "    T{ sort-me f 2 3 } T{ sort-me f 3 2 }"
30         "    T{ sort-me f 4 3 } T{ sort-me f 2 1 }"
31         "}"
32         "{ { a>> <=> } { b>> >=< } } sort-by ."
33         "{\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}"
34     }
35 } ;
36
37 ARTICLE: "sorting.slots" "Sorting by slots"
38 "The " { $vocab-link "sorting.slots" } " vocabulary can sort tuples by slot in ascending or descending order, using subsequent slots as tie-breakers." $nl
39 "Comparing two objects by a sequence of slots:"
40 { $subsections compare-slots }
41 "Sorting a sequence of tuples by a slot/comparator pairs:"
42 { $subsections
43     sort-by
44     sort-keys-by
45     sort-values-by
46 } ;
47
48 ABOUT: "sorting.slots"