]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.extras: adding a (slow) arg-sort.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 30 Oct 2012 00:45:33 +0000 (17:45 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 30 Oct 2012 00:45:33 +0000 (17:45 -0700)
extra/sequences/extras/extras-tests.factor
extra/sequences/extras/extras.factor

index 8e4fee89d8747e5df9f43399cfd081c3779566a5..797a5334f4f750448a3695c745f92535381a1d2d 100644 (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
index 93e1555e94100a1fd534ec2622a6c30b79276496..15f7f85bd26762f6c948a646b69bba7660579f11 100644 (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