]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.extras: faster arg-min and arg-max
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 31 Jul 2022 18:19:14 +0000 (11:19 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 31 Jul 2022 18:19:14 +0000 (11:19 -0700)
it doesn't have to iterate through the sequence twice now

extra/sequences/extras/extras.factor

index 782b7bea7358524759da0723e4296054ed5ba522..9d6de7a958e89ad742c4b9844b27ccd9509d280a 100644 (file)
@@ -432,12 +432,6 @@ INSTANCE: odds virtual-sequence
 : 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 )
@@ -625,6 +619,12 @@ PRIVATE>
 : 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