From: daniel@...42.com Date: 2020-04-24T00:19:36+00:00 Subject: [ruby-core:98039] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence Issue #16812 has been updated by Dan0042 (Daniel DeLorme). Theoretically I'm in favor but there's some edge cases that need consideration. ```ruby nums = (0..20).to_a s = 10.step(by: -2) # 10, 8, 6, 4, 2, 0, -2, ... nums[s] #=> [10, 8, 6, 4, 2, 0, 19, 17, ...] ??? s = (-5..5) % 2 # -5, -3, -1, 1, 3, 5 nums[s] #=> [16, 18, 20, 1, 3, 5] ??? ``` ---------------------------------------- Feature #16812: Allow slicing arrays with ArithmeticSequence https://bugs.ruby-lang.org/issues/16812#change-85267 * Author: zverok (Victor Shepelev) * Status: Open * Priority: Normal ---------------------------------------- I believe when concepts of ArithmeticSequence and `Range#%` were introduced, one of the main intended usages was array slicing in scientific data processing. So, it seems to make sense to allow this in `Array#[]`: ```ruby ary[(5..20) % 2] # each second element between 5 and 20 ary[(0..) % 3] # each third element ary[10.step(by: -1)] # elements 10, 9, 8, 7 .... ``` PR is [here](https://github.com/ruby/ruby/pull/3055). My reasoning is as follows: 1. As stated above, ArithmeticSequence and `Range#%` seem to have been introduced exactly for this goal 2. Python has its slicing syntax as `begin:end:step` (with a possibility to omit either), and it seems to be well respected and used feature for data processing. So I believe it is useful, and relatively easy to integrate into existing functionality I expect the usual "it is ugly and unreadable!" backlash. I don't have an incentive, nor energy, to "defend" the proposal, so I would not. -- https://bugs.ruby-lang.org/ Unsubscribe: