sorting.extras: adding map-sort for efficiently decorating, sorting, and undecorating a sequence.

db4
John Benediktsson 2013-04-01 18:35:05 -07:00
parent fdc43d0574
commit 9d6f2ad353
2 changed files with 7 additions and 1 deletions

View File

@ -1,7 +1,9 @@
USING: kernel math.order sequences tools.test ; USING: kernel math math.order sequences tools.test ;
IN: sorting.extras IN: sorting.extras
{ { 0 2 1 } } [ { 10 30 20 } [ <=> ] argsort ] unit-test { { 0 2 1 } } [ { 10 30 20 } [ <=> ] argsort ] unit-test
{ { 2 0 1 } } [ { { 2 0 1 } } [
{ "hello" "goodbye" "yo" } [ [ length ] bi@ <=> ] argsort { "hello" "goodbye" "yo" } [ [ length ] bi@ <=> ] argsort
] unit-test ] unit-test
{ 1 { 2 3 4 5 } } [ 1 { 1 2 3 4 } [ dupd + ] map-sort ] unit-test

View File

@ -5,3 +5,7 @@ IN: sorting.extras
[ dup length iota zip ] dip [ dup length iota zip ] dip
[ [ first-unsafe ] bi@ ] prepose [ [ first-unsafe ] bi@ ] prepose
sort [ second-unsafe ] map! ; inline sort [ second-unsafe ] map! ; inline
: map-sort ( ... seq quot: ( ... elt -- ... key ) -- ... sortedseq )
[ map ] curry keep zip [ second-unsafe ] sort-with
[ first-unsafe ] map ; inline