From: "boris_stitnicky (Boris Stitnicky)" Date: 2013-10-26T12:43:13+09:00 Subject: [ruby-core:58042] [ruby-trunk - Feature #9049] Shorthands (a:b, *) for inclusive indexing Issue #9049 has been updated by boris_stitnicky (Boris Stitnicky). @david_macmahon, mohawkjohn: Colon is busy, how about harassing % ? %s/1 1e6 step 12/ # %s would mean series, returning an enumerator As far as slicing (multidimensional) matrices is involved, I need to already get myself together and join NMatrix team :-), but seriously, you need an object for that: class Matrix::Knife # here you define, in each dimension, what slices you take and what you drop end And then, you need to do parametrized subclassing of Matrix for each dimensionality (1D matrices aka. vectors, common 2D matrices, 3D matrices etc.), and then, each such parametrized subclass nedds to own its own parametrized subclass of Matrix::Knife. And then you need a cool constructor for those knives, and that can even be a string: xxx = Matrix.D3.Knife( "*|1:3|0+3+5" ) # knife xxx takes all the ranks in dimension 1, ranks 1..3 in dimension 2, and ranks 0, 3 and 5 # in dimension 3, and when it cuts, it produces an instance of Matrix.D3 parametrized subclass # of Matrix class. That's how I'd see it. m.slice( xxx ) # slicing 3D matrix m with knife xxx xxx.cut( m ) # same as above, with reversed roles or the receiver and the argument I'm not really sure there is need to bother matz for novel syntax, but if yes, it would be the syntax for those knives: %X[ * | 1:3 | 0+3+5 ] You could cut arrays with them too my_knife = %X[ 1 + 3:5 ] ( 1 .. 6 ).to_a.slice( my_knife ) #=> [2, 4, 5, 6] # just like ( 1 .. 6 ).to_a.values_at *[ 1, *3..5 ] ---------------------------------------- Feature #9049: Shorthands (a:b, *) for inclusive indexing https://bugs.ruby-lang.org/issues/9049#change-42621 Author: mohawkjohn (John Woods) Status: Open Priority: Low Assignee: Category: core Target version: =begin For NMatrix, we've implemented a range shorthand which relies on Hashes: (({m[1=>3,2=>4]})), for example, which returns rows 1 through 3 inclusive of columns 2 through 4 (also inclusive). The original goal was to be able to do (({m[1:3,2:4]})) using the new hash notation, but the new hash notation requires that the key be a symbol ��� it won't accept an integer. Whether through the hash interface or not, it'd be lovely if there were a shorthand for slicing matrices (and even Ruby Arrays) using colon. This could just be an alternate syntax for ranges, also ��� which might make more sense. The other related shorthand we'd love to find a way to implement is the all-inclusive shorthand. It gets to be a pain to type (({n[0...n.shape[0],1...3]})) to get a submatrix (a slice), and it's really difficult to read. As a work-around, we currently use the (({:*})) symbol: (({n[:*,1...3]})). But it'd be simpler if there were a way to use a splat operator without an operand as a function argument. It might be a special case where the (({*})) is treated as a (({:*})) automatically. But this edge case might cause confusion with error messages when users make syntax errors elsewhere. The colon shorthand is the highest priority for us. =end -- http://bugs.ruby-lang.org/