]> gitweb.factorcode.org Git - factor.git/commitdiff
sorting.extras: adding map-sort for efficiently decorating, sorting, and undecorating...
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 2 Apr 2013 01:35:05 +0000 (18:35 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 2 Apr 2013 01:35:05 +0000 (18:35 -0700)
extra/sorting/extras/extras-tests.factor
extra/sorting/extras/extras.factor

index 931ff3ec52aba6e2ee5e4a395a4718aa4b9995e1..49916e6248a867e596377f7e679e70e72b85b984 100644 (file)
@@ -1,7 +1,9 @@
-USING: kernel math.order sequences tools.test ;
+USING: kernel math 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
+
+{ 1 { 2 3 4 5 } } [ 1 { 1 2 3 4 } [ dupd + ] map-sort ] unit-test
index 2c329fb0f9a3c46211a5813612d88aa7395b0fe7..72d2e1574fca44b36c811395bfbcdde83c268101 100644 (file)
@@ -5,3 +5,7 @@ IN: sorting.extras
     [ dup length iota zip ] dip
     [ [ first-unsafe ] bi@ ] prepose
     sort [ second-unsafe ] map! ; inline
+
+: map-sort ( ... seq quot: ( ... elt -- ... key ) -- ... sortedseq )
+    [ map ] curry keep zip [ second-unsafe ] sort-with
+    [ first-unsafe ] map ; inline