{ -2 } [ { -3 -2 } max-subarray-sum ] unit-test
{ 7 } [ { 1 2 3 -4 5 } max-subarray-sum ] unit-test
{ 6 } [ { 1 2 3 -4 1 1 } max-subarray-sum ] unit-test
+
+{ { 9 7 5 } } [ -1 -6 -2 10 <iota> <step-slice> >array ] unit-test
+{ { 9 7 } } [ -1 -5 -2 10 <iota> <step-slice> >array ] unit-test
+{ { 9 7 } } [ -1 -4 -2 10 <iota> <step-slice> >array ] unit-test
+{ { 9 } } [ -1 -3 -2 10 <iota> <step-slice> >array ] unit-test
+{ { } } [ -4 10 -2 10 <iota> <step-slice> >array ] unit-test
+{ { 6 8 } } [ -4 15 2 10 <iota> <step-slice> >array ] unit-test
+{ { 1 3 } } [ 1 4 2 10 <iota> <step-slice> >array ] unit-test
+{ { 1 3 } } [ 1 5 2 10 <iota> <step-slice> >array ] unit-test
+{ { 1 3 5 } } [ 1 6 2 10 <iota> <step-slice> >array ] unit-test
:: subseq* ( from to seq -- subseq )
seq length :> len
from [ dup 0 < [ len + ] when ] [ 0 ] if*
- to [ dup 0 < [ len + ] when ] [ len ] if*
- [ 0 len clamp ] bi@ dupd max seq subseq ;
+ to [ dup 0 < [ len + ] when ] [ len ] if* [ 0 len clamp ] bi@ dupd max seq subseq ;
: safe-subseq ( from to seq -- subseq )
[ length '[ 0 _ clamp ] bi@ ] keep subseq ;
: max-subarray-sum ( seq -- sum )
[ -1/0. 0 ] dip
[ [ + ] keep max [ max ] keep ] each drop ;
+
+TUPLE: step-slice
+ { from integer read-only initial: 0 }
+ { to integer read-only initial: 0 }
+ { seq read-only }
+ { step integer read-only } ;
+
+:: <step-slice> ( from to step seq -- step-slice )
+ step zero? [ "can't be zero" throw ] when
+ seq length :> len
+ step 0 > [
+ from [ 0 ] unless*
+ to [ len ] unless*
+ ] [
+ from [ len ] unless*
+ to [ 0 ] unless*
+ ] if
+ [ dup 0 < [ len + ] when 0 len clamp ] bi@
+ ! FIXME: make this work with steps
+ seq dup slice? [ collapse-slice ] when
+ step step-slice boa ;
+
+M: step-slice virtual-exemplar seq>> ; inline
+
+M: step-slice virtual@
+ [ step>> * ] [ from>> + ] [ seq>> ] tri ; inline
+
+M: step-slice length
+ [ to>> ] [ from>> - ] [ step>> ] tri
+ dup 0 < [ [ neg 0 max ] dip neg ] when /mod
+ zero? [ 1 + ] unless ; inline
+
+INSTANCE: step-slice virtual-sequence
+++ /dev/null
-USING: arrays sequences tensors.tensor-slice tools.test ;
-IN: tensors.tensor-slice.tests
-
-{ { 9 7 5 } } [ -1 -6 -2 10 <iota> <step-slice> >array ] unit-test
-{ { 9 7 } } [ -1 -5 -2 10 <iota> <step-slice> >array ] unit-test
-{ { 9 7 } } [ -1 -4 -2 10 <iota> <step-slice> >array ] unit-test
-{ { 9 } } [ -1 -3 -2 10 <iota> <step-slice> >array ] unit-test
-{ { } } [ -4 10 -2 10 <iota> <step-slice> >array ] unit-test
-{ { 6 8 } } [ -4 15 2 10 <iota> <step-slice> >array ] unit-test
-{ { 1 3 } } [ 1 4 2 10 <iota> <step-slice> >array ] unit-test
-{ { 1 3 } } [ 1 5 2 10 <iota> <step-slice> >array ] unit-test
-{ { 1 3 5 } } [ 1 6 2 10 <iota> <step-slice> >array ] unit-test
\ No newline at end of file
+++ /dev/null
-USING: accessors kernel math math.order sequences ;
-IN: tensors.tensor-slice
-
-TUPLE: step-slice
- { from integer read-only initial: 0 }
- { to integer read-only initial: 0 }
- { seq read-only }
- { step integer read-only } ;
-
-:: <step-slice> ( from to step seq -- step-slice )
- step zero? [ "can't be zero" throw ] when
- seq length :> len
- step 0 > [
- from [ 0 ] unless*
- to [ len ] unless*
- ] [
- from [ len ] unless*
- to [ 0 ] unless*
- ] if
- [ dup 0 < [ len + ] when 0 len clamp ] bi@
- ! FIXME: make this work with steps
- seq dup slice? [ collapse-slice ] when
- step step-slice boa ;
-
-M: step-slice virtual-exemplar seq>> ; inline
-
-M: step-slice virtual@
- [ step>> * ] [ from>> + ] [ seq>> ] tri ; inline
-
-M: step-slice length
- [ to>> ] [ from>> - ] [ step>> ] tri
- dup 0 < [ [ neg 0 max ] dip neg ] when /mod
- zero? [ 1 + ] unless ; inline
-
-INSTANCE: step-slice virtual-sequence