]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.extras: use 0reduce for max-subarray-sum and make it row-polymorphic
authorDoug Coleman <doug.coleman@gmail.com>
Mon, 8 Jan 2024 21:02:52 +0000 (15:02 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Wed, 10 Jan 2024 19:54:21 +0000 (13:54 -0600)
otherwise the compiled word errors since stack effect is ( x x x -- x x )

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 }