]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/sequences/extras/extras.factor
sequences.extras: use 0reduce for max-subarray-sum and make it row-polymorphic
[factor.git] / extra / sequences / extras / extras.factor
index 05cf6cdf76e6cdd8c9b77e4abb85abf3bc2236b0..9fbd24341184002805f18b14e78d66bb4bb7b05a 100644 (file)
@@ -350,13 +350,13 @@ PRIVATE>
 : progressive-index ( seq1 seq2 -- hash seq' )
     [ ] progressive-index-by ; inline
 
-: 0reduce ( seq quot: ( prev elt -- next ) -- result )
+: 0reduce ( seq quot: ( ..a prev elt -- ..a next ) -- result )
     [ 0 ] dip reduce ; inline
 
 : ?unclip ( seq -- rest/f first/f )
     [ f f ] [ unclip ] if-empty ;
 
-: 1reduce ( seq quot: ( prev elt -- next ) -- result )
+: 1reduce ( seq quot: ( ..a prev elt -- ..a next ) -- result )
     [ f ] swap '[ [ ] _ map-reduce ] if-empty ; inline
 
 <PRIVATE
@@ -1031,8 +1031,8 @@ ALIAS: map-infimum map-minimum deprecated
 ! https://en.wikipedia.org/wiki/Maximum_subarray_problem
 ! Kadane's algorithm O(n) largest sum in subarray
 : max-subarray-sum ( seq -- sum )
-    [ -1/0. ] dip
-    [ [ + ] keep max [ max ] keep ] each drop ;
+    [ -1/0. ] dip
+    [ [ + ] keep max [ max ] keep ] 0reduce drop ;
 
 TUPLE: step-slice
     { from integer read-only initial: 0 }