[ 4 ] [ 5 iota [ ] supremum-by ] unit-test
[ 0 ] [ 5 iota [ ] infimum-by ] unit-test
+{ "bar" } [ { "bar" "baz" "qux" } [ length ] supremum-by ] unit-test
+{ "bar" } [ { "bar" "baz" "qux" } [ length ] infimum-by ] unit-test
[ { "foo" } ] [ { { "foo" } { "bar" } } [ first ] supremum-by ] unit-test
[ { "bar" } ] [ { { "foo" } { "bar" } } [ first ] infimum-by ] unit-test
{ { } } [ { } <odds> >array ] unit-test
{ { 1 3 } } [ 5 iota <odds> >array ] unit-test
{ { 1 3 5 } } [ 6 iota <odds> >array ] unit-test
+
+{ 1 } [ { 1 7 3 7 6 3 7 } arg-max ] unit-test
+{ 0 } [ { 1 7 3 7 6 3 7 } arg-min ] unit-test
-USING: accessors arrays grouping kernel locals math math.order
-math.ranges sequences sequences.private splitting ;
+USING: accessors arrays assocs grouping kernel locals math
+math.order math.ranges sequences sequences.private splitting ;
FROM: sequences => change-nth ;
IN: sequences.extras
: supremum-by ( seq quot: ( ... elt -- ... x ) -- elt )
[ [ first dup ] dip call ] 2keep [
- dupd call pick dupd max over =
+ dupd call pick dupd after?
[ [ 2drop ] 2dip ] [ 2drop ] if
] curry 1 each-from drop ; inline
: infimum-by ( seq quot: ( ... elt -- ... x ) -- elt )
[ [ first dup ] dip call ] 2keep [
- dupd call pick dupd min over =
+ dupd call pick dupd before?
[ [ 2drop ] 2dip ] [ 2drop ] if
] curry 1 each-from drop ; inline
: until-empty ( seq quot -- )
[ dup empty? ] swap until drop ; inline
+
+: arg-max ( seq -- n )
+ dup length iota zip [ first-unsafe ] supremum-by second ;
+
+: arg-min ( seq -- n )
+ dup length iota zip [ first-unsafe ] infimum-by second ;