# HG changeset patch # User Chris Cannam # Date 1369503070 -3600 # Node ID 39b5f3fed12d2c2662a04ae0a29ef6bd9ce8187b # Parent 2c3faf6a2820175e3cd7dcb819419fc26de3e0e4 Make slice return partial slices when range extents overlap vector ends diff -r 2c3faf6a2820 -r 39b5f3fed12d yetilab/vector.yeti --- a/yetilab/vector.yeti Sat May 25 18:18:10 2013 +0100 +++ b/yetilab/vector.yeti Sat May 25 18:31:10 2013 +0100 @@ -72,7 +72,15 @@ //!!! doc note: argument order chosen for consistency with std module function slice v start end is ~double[] -> number -> number -> ~double[] = - Arrays#copyOfRange(v, start, end); + if start < 0 then slice v 0 end + elif start > length' v then slice v (length' v) end + else + if end < start then slice v start start + elif end > length' v then slice v start (length' v) + else + Arrays#copyOfRange(v, start, end); + fi + fi; resizedTo n v is number -> ~double[] -> ~double[] = Arrays#copyOf(v, n); diff -r 2c3faf6a2820 -r 39b5f3fed12d yetilab/vector/test/test_vector.yeti --- a/yetilab/vector/test/test_vector.yeti Sat May 25 18:18:10 2013 +0100 +++ b/yetilab/vector/test/test_vector.yeti Sat May 25 18:31:10 2013 +0100 @@ -78,9 +78,12 @@ "slice": \( v = vec.fromList [1,2,3,4]; - vec.equal (vec.slice v 0 4) v and ( - vec.equal (vec.slice v 2 4) (vec.fromList [3,4]) - ) + vec.equal (vec.slice v 0 4) v and + vec.equal (vec.slice v 2 4) (vec.fromList [3,4]) and + vec.equal (vec.slice v (-1) 2) (vec.fromList [1,2]) and + vec.equal (vec.slice v 3 5) (vec.fromList [4]) and + vec.equal (vec.slice v 5 7) (vec.fromList []) and + vec.equal (vec.slice v 3 2) (vec.fromList []) ), "resizedTo": \(