it doesn't have to iterate through the sequence twice now
: until-empty ( seq quot -- )
[ dup empty? ] swap until drop ; inline
-: arg-max ( seq -- n )
- [ supremum ] keep index ;
-
-: arg-min ( seq -- n )
- [ infimum ] keep index ;
-
<PRIVATE
: push-index-if ( ..a elt i quot: ( ..a elt -- ..b ? ) accum -- ..b )
: infimum-by* ( ... seq quot: ( ... elt -- ... x ) -- ... i elt )
[ before? ] select-by* ; inline
+: arg-max ( seq -- n )
+ [ ] supremum-by* drop ;
+
+: arg-min ( seq -- n )
+ [ ] infimum-by* drop ;
+
: ?supremum ( seq/f -- elt/f )
[ f ] [
[ ] [ 2dup and [ max ] [ dupd ? ] if ] map-reduce