sequences.extras: this formulation of arg-max/min should be better

factor-shell
Björn Lindqvist 2018-02-08 19:39:31 +01:00
parent cb98fd07c6
commit 603354a02e
3 changed files with 13 additions and 2 deletions

View File

@ -230,6 +230,16 @@ HELP: start-all*
"{ 0 2 }"
} } ;
HELP: arg-max
{ $values { "seq" sequence } { "n" integer } }
{ $description "Outputs the sequence with the largest item." } ;
HELP: arg-min
{ $values { "seq" sequence } { "n" integer } }
{ $description "Outputs the sequence with the smallest item." } ;
{ arg-max arg-min } related-words
HELP: count-subseq
{ $values
{ "subseq" sequence } { "seq" sequence } { "n" integer } }

View File

@ -157,6 +157,7 @@ IN: sequences.extras.tests
{ { 1 3 5 } } [ 6 <iota> <odds> >array ] unit-test
{ 1 } [ { 1 7 3 7 6 3 7 } arg-max ] unit-test
{ 2 } [ { 0 1 99 } arg-max ] unit-test
{ 0 } [ { 1 7 3 7 6 3 7 } arg-min ] unit-test
{ V{ 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

View File

@ -411,10 +411,10 @@ INSTANCE: odds immutable-sequence
[ dup empty? ] swap until drop ; inline
: arg-max ( seq -- n )
<enumerated> [ second-unsafe ] supremum-by first ;
[ supremum ] keep index ;
: arg-min ( seq -- n )
<enumerated> [ second-unsafe ] infimum-by first ;
[ infimum ] keep index ;
<PRIVATE