sequences.extras: adding a (slow) arg-sort.

db4
John Benediktsson 2012-10-29 17:45:33 -07:00
parent 1a5f2ad961
commit 4bdb40574a
2 changed files with 6 additions and 1 deletions

View File

@ -124,6 +124,8 @@ IN: sequences.extras.tests
{ 1 } [ { 1 7 3 7 6 3 7 } arg-max ] unit-test
{ 0 } [ { 1 7 3 7 6 3 7 } arg-min ] unit-test
{ { 0 4 } } [ { 5 3 2 10 5 } [ 5 = ] arg-where ] unit-test
{ { 2 1 0 4 3 } } [ { 5 3 2 10 5 } arg-sort ] unit-test
{ t } [ { 1 2 3 4 5 } 1 first= ] unit-test
{ t } [ { 1 2 3 4 5 } 2 second= ] unit-test

View File

@ -1,6 +1,6 @@
USING: accessors arrays assocs fry grouping growable kernel
locals make math math.order math.ranges sequences
sequences.private splitting ;
sequences.private sorting splitting ;
FROM: sequences => change-nth ;
IN: sequences.extras
@ -319,6 +319,9 @@ INSTANCE: odds immutable-sequence
[ dup length iota zip ] dip
[ first-unsafe ] prepose filter values ; inline
: arg-sort ( seq -- indices )
dup length iota zip sort-keys values ;
: first= ( seq elt -- ? ) [ first ] dip = ; inline
: second= ( seq elt -- ? ) [ second ] dip = ; inline
: third= ( seq elt -- ? ) [ third ] dip = ; inline