Merge branch 'master' of git://factorcode.org/git/factor

db4
Slava Pestov 2009-04-03 20:09:00 -05:00
commit 48aa95e5b1
4 changed files with 42 additions and 8 deletions

View File

@ -14,7 +14,7 @@ HELP: compare-slots
HELP: sort-by-slots
{ $values
{ "seq" sequence } { "sort-specs" "a sequence of accessors ending with a comparator" }
{ "seq'" sequence }
{ "sortedseq" sequence }
}
{ $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." }
{ $examples
@ -39,11 +39,20 @@ HELP: split-by-slots
}
{ $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." } ;
HELP: sort-by
{ $values
{ "seq" sequence } { "sort-seq" "a sequence of comparators" }
{ "sortedseq" sequence }
}
{ $description "Sorts a sequence by comparing elements by comparators, using subsequent comparators when there is a tie." } ;
ARTICLE: "sorting.slots" "Sorting by slots"
"The " { $vocab-link "sorting.slots" } " vocabulary can sort tuples by slot in ascending or descending order, using subsequent slots as tie-breakers." $nl
"Comparing two objects by a sequence of slots:"
{ $subsection compare-slots }
"Sorting a sequence by a sequence of slots:"
{ $subsection sort-by-slots } ;
"Sorting a sequence of tuples by a slot/comparator pairs:"
{ $subsection sort-by-slots }
"Sorting a sequence by a sequence of comparators:"
{ $subsection sort-by } ;
ABOUT: "sorting.slots"

View File

@ -1,7 +1,8 @@
! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors math.order sorting.slots tools.test
sorting.human arrays sequences kernel assocs multiline ;
sorting.human arrays sequences kernel assocs multiline
sorting.functor ;
IN: sorting.literals.tests
TUPLE: sort-test a b c tuple2 ;
@ -76,6 +77,9 @@ TUPLE: tuple2 d ;
[ { } ]
[ { } { { a>> <=> } { b>> >=< } { c>> <=> } } sort-by-slots ] unit-test
[ { } ]
[ { } { } sort-by-slots ] unit-test
[
{
T{ sort-test { a 6 } { tuple2 T{ tuple2 { d 1 } } } }
@ -143,3 +147,15 @@ TUPLE: tuple2 d ;
T{ sort-test { a 5 } { tuple2 T{ tuple2 { d 4 } } } }
} { { tuple2>> d>> } { a>> } } split-by-slots [ >array ] map
] unit-test
[ { "a" "b" "c" } ] [ { "b" "c" "a" } { <=> <=> } sort-by ] unit-test
[ { "b" "c" "a" } ] [ { "b" "c" "a" } { } sort-by ] unit-test
<< "length-test" [ length ] define-sorting >>
[ { { 1 } { 1 2 3 } { 1 3 2 } { 3 2 1 } } ]
[
{ { 3 2 1 } { 1 2 3 } { 1 3 2 } { 1 } }
{ length-test<=> <=> } sort-by
] unit-test

View File

@ -7,24 +7,33 @@ IN: sorting.slots
<PRIVATE
: short-circuit-comparator ( word -- quot )
execute dup +eq+ eq? [ drop f ] when ; inline
: slot-comparator ( seq -- quot )
[
but-last-slice
[ '[ [ _ execute ] bi@ ] ] map concat
] [
peek
'[ @ _ execute dup +eq+ eq? [ drop f ] when ]
'[ @ _ short-circuit-comparator ]
] bi ;
PRIVATE>
MACRO: compare-slots ( sort-specs -- <=> )
#! sort-spec: { accessors comparator }
'[ _ [ slot-comparator ] map 2|| +eq+ or ] ;
[ slot-comparator ] map '[ _ 2|| +eq+ or ] ;
: sort-by-slots ( seq sort-specs -- seq' )
: sort-by-slots ( seq sort-specs -- sortedseq )
'[ _ compare-slots ] sort ;
MACRO: compare-seq ( seq -- quot )
[ '[ _ short-circuit-comparator ] ] map '[ _ 2|| +eq+ or ] ;
: sort-by ( seq sort-seq -- sortedseq )
'[ _ compare-seq ] sort ;
MACRO: split-by-slots ( accessor-seqs -- quot )
[ [ '[ [ _ execute ] bi@ ] ] map concat [ = ] compose ] map
'[ [ _ 2&& ] slice monotonic-slice ] ;

View File

@ -84,7 +84,7 @@ TUPLE: unique-deque assoc deque ;
: normalize-hrefs ( links spider -- links' )
currently-spidering>> present swap
[ dup url-absolute? [ derive-url ] [ url-append-path >url ] if ] with map ;
[ [ >url ] bi@ derive-url ] with map ;
: print-spidering ( url depth -- )
"depth: " write number>string write