sorting.extras: adding 'argsort'.

db4
John Benediktsson 2012-08-06 10:59:40 -07:00
parent 04794fe715
commit dfe06d3070
2 changed files with 14 additions and 0 deletions

View File

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

View File

@ -0,0 +1,7 @@
USING: assocs kernel sequences sequences.private sorting ;
IN: sorting.extras
: argsort ( seq quot: ( obj1 obj2 -- <=> ) -- sortedseq )
[ dup length iota zip ] dip
[ [ first-unsafe ] bi@ ] prepose
sort [ 1 swap nth-unsafe ] map! ; inline