ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:98034] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence
@ 2020-04-23 15:32 zverok.offline
  2020-04-23 20:04 ` [ruby-core:98036] " eregontp
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: zverok.offline @ 2020-04-23 15:32 UTC (permalink / raw
  To: ruby-core

Issue #16812 has been reported by zverok (Victor Shepelev).

----------------------------------------
Feature #16812: Allow slicing arrays with ArithmeticSequence
https://bugs.ruby-lang.org/issues/16812

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:98036] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence
  2020-04-23 15:32 [ruby-core:98034] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence zverok.offline
@ 2020-04-23 20:04 ` eregontp
  2020-04-23 20:44 ` [ruby-core:98037] " zverok.offline
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: eregontp @ 2020-04-23 20:04 UTC (permalink / raw
  To: ruby-core

Issue #16812 has been updated by Eregon (Benoit Daloze).


Rather neutral on this, but would you want that to work for `Array#[]=` too?
I would be against `Array#[]=` as it's already so complicated and that would just make it a lot more so.

In `Array#[]` it's probably fine though.

----------------------------------------
Feature #16812: Allow slicing arrays with ArithmeticSequence
https://bugs.ruby-lang.org/issues/16812#change-85265

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:98037] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence
  2020-04-23 15:32 [ruby-core:98034] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence zverok.offline
  2020-04-23 20:04 ` [ruby-core:98036] " eregontp
@ 2020-04-23 20:44 ` zverok.offline
  2020-04-24  0:19 ` [ruby-core:98039] " daniel
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: zverok.offline @ 2020-04-23 20:44 UTC (permalink / raw
  To: ruby-core

Issue #16812 has been updated by zverok (Victor Shepelev).


@Eregon, I wanted at first to see what people say about this one :) 

`Array#[]=` is a thing that should be kinda "symmetric", but playing a bit with it, I understood that I am afraid of trying to guess what would be "logical". 

Honestly, I can't remember I've ever used a form like `a[1..3] = 'x'`, and its behavior is kinda "theoretically logical", but at the same time only one of the things you may "intuitively" expect ("replace all three elements with one, changing array's size" wouldn't be my first guess...). 

So, at least for now, my only proposal is `Array#[]`.

----------------------------------------
Feature #16812: Allow slicing arrays with ArithmeticSequence
https://bugs.ruby-lang.org/issues/16812#change-85266

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:98039] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence
  2020-04-23 15:32 [ruby-core:98034] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence zverok.offline
  2020-04-23 20:04 ` [ruby-core:98036] " eregontp
  2020-04-23 20:44 ` [ruby-core:98037] " zverok.offline
@ 2020-04-24  0:19 ` daniel
  2020-04-24  4:08 ` [ruby-core:98041] " nobu
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: daniel @ 2020-04-24  0:19 UTC (permalink / raw
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:98041] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence
  2020-04-23 15:32 [ruby-core:98034] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence zverok.offline
                   ` (2 preceding siblings ...)
  2020-04-24  0:19 ` [ruby-core:98039] " daniel
@ 2020-04-24  4:08 ` nobu
  2020-04-24  5:09 ` [ruby-core:98042] " muraken
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: nobu @ 2020-04-24  4:08 UTC (permalink / raw
  To: ruby-core

Issue #16812 has been updated by nobu (Nobuyoshi Nakada).


A few bugs.

* `Float` `ArithmeticSequence` crashes.

    ```shell
    $ ./ruby -e '[*0..10][(0.0..)%10]'
    Assertion Failed: ../src/include/ruby/3/arithmetic/long.h:136:ruby3_fix2long_by_shift:"RB_FIXNUM_P(x)"
    ```

* If overridden `take_while` (and `drop_while`) returns non-`Array`, crashes.

    ```shell
    $ ./ruby 'a = (1..10)%2; def a.take_while; nil; end; [*1..10][a]'
    -e:1:in `<main>': wrong argument type nil (expected Array) (TypeError)
    ```

These resulted in assertion failures, but would segfault when compiled with `NDEBUG`.


----------------------------------------
Feature #16812: Allow slicing arrays with ArithmeticSequence
https://bugs.ruby-lang.org/issues/16812#change-85271

* 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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:98042] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence
  2020-04-23 15:32 [ruby-core:98034] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence zverok.offline
                   ` (3 preceding siblings ...)
  2020-04-24  4:08 ` [ruby-core:98041] " nobu
@ 2020-04-24  5:09 ` muraken
  2020-04-25 17:48 ` [ruby-core:98065] " zverok.offline
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: muraken @ 2020-04-24  5:09 UTC (permalink / raw
  To: ruby-core

Issue #16812 has been updated by mrkn (Kenta Murata).

Assignee set to matz (Yukihiro Matsumoto)
Status changed from Open to Assigned

I'm positive this if the behavior is the same as Python's list slicing.
If the behavior will be different from Python's, I'm negative because it confuses PyCall users.

----------------------------------------
Feature #16812: Allow slicing arrays with ArithmeticSequence
https://bugs.ruby-lang.org/issues/16812#change-85272

* Author: zverok (Victor Shepelev)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:98065] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence
  2020-04-23 15:32 [ruby-core:98034] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence zverok.offline
                   ` (4 preceding siblings ...)
  2020-04-24  5:09 ` [ruby-core:98042] " muraken
@ 2020-04-25 17:48 ` zverok.offline
  2020-06-18  2:06 ` [ruby-core:98846] " muraken
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: zverok.offline @ 2020-04-25 17:48 UTC (permalink / raw
  To: ruby-core

Issue #16812 has been updated by zverok (Victor Shepelev).


As there is no immediate rejection, I updated the implementation, making it more robust.

@Dan0042, I tried to make edge cases consistent, so now they are...

```ruby
(0..20).to_a[10.step(by: -2)]
# => [10, 8, 6, 4, 2, 0] -- avoids weird cycling
(0..20).to_a[(-5..5) % 2]
# => [] -- this is consistent with
(0..20).to_a[-5..5] # which can be thought as (-5..5) % 1
# => [] 
# Note, though:
(0..20).to_a[-19..5]
# => [2, 3, 4, 5] -- not literally "from -19 to 5", but "from 19th from the end to 5th from the beginning"
# ...so...
(0..20).to_a[(-19..5)%2]
# => [2, 4]
```

@nobu I've tried to fix bugs. Now float begin/end is processed correctly, float step is TypeError, and the code does not rely on `#take_while`/`#drop_while`.

@mrkn I've checked against Python impl, and believe the behavior is mostly the same. One difference I am aware of is this:

Python:
```python
list(range(10))[-100:100:2]                                                                                                            
#=> [0, 2, 4, 6, 8]
```
Ruby:
```ruby
[*0..10][(-100..100)%2]
# => nil
```
That's because first of all I wanted to make it consistent with
```ruby
[*0..10][-100..100]
# => nil
```
...which may be questioned (like, "range from -100 to 100 includes 0..10, so it should fetch entire array"), but that's how it is now :)

----------------------------------------
Feature #16812: Allow slicing arrays with ArithmeticSequence
https://bugs.ruby-lang.org/issues/16812#change-85291

* Author: zverok (Victor Shepelev)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:98846] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence
  2020-04-23 15:32 [ruby-core:98034] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence zverok.offline
                   ` (5 preceding siblings ...)
  2020-04-25 17:48 ` [ruby-core:98065] " zverok.offline
@ 2020-06-18  2:06 ` muraken
  2020-06-19 14:12 ` [ruby-core:98886] " muraken
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: muraken @ 2020-06-18  2:06 UTC (permalink / raw
  To: ruby-core

Issue #16812 has been updated by mrkn (Kenta Murata).


It may be better to change the behavior of `[*0..10][-100..100]` because `[*0..10][..100]` does not return `nil`:

```
[*0..10][..100]
# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
```

And the following cases seems inconsistent to me:

```
[*0..10][0..12]
# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

[*0..10][-12..-1]
# => nil
```

----------------------------------------
Feature #16812: Allow slicing arrays with ArithmeticSequence
https://bugs.ruby-lang.org/issues/16812#change-86209

* Author: zverok (Victor Shepelev)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:98886] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence
  2020-04-23 15:32 [ruby-core:98034] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence zverok.offline
                   ` (6 preceding siblings ...)
  2020-06-18  2:06 ` [ruby-core:98846] " muraken
@ 2020-06-19 14:12 ` muraken
  2020-06-26 19:08 ` [ruby-core:98963] " daniel
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: muraken @ 2020-06-19 14:12 UTC (permalink / raw
  To: ruby-core

Issue #16812 has been updated by mrkn (Kenta Murata).


I made a patch: https://github.com/ruby/ruby/pull/3241

----------------------------------------
Feature #16812: Allow slicing arrays with ArithmeticSequence
https://bugs.ruby-lang.org/issues/16812#change-86263

* Author: zverok (Victor Shepelev)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:98963] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence
  2020-04-23 15:32 [ruby-core:98034] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence zverok.offline
                   ` (7 preceding siblings ...)
  2020-06-19 14:12 ` [ruby-core:98886] " muraken
@ 2020-06-26 19:08 ` daniel
  2020-06-27  9:14 ` [ruby-core:98979] " zverok.offline
  2020-07-20  7:04 ` [ruby-core:99235] " matz
  10 siblings, 0 replies; 12+ messages in thread
From: daniel @ 2020-06-26 19:08 UTC (permalink / raw
  To: ruby-core

Issue #16812 has been updated by Dan0042 (Daniel DeLorme).


mrkn (Kenta Murata) wrote in #note-7:
> It may be better to change the behavior of `[*0..10][-100..100]`

I somewhat agree with that. When using range slicing most combinations make sense:

```ruby
[*0..10][0..4]   #first elements
[*0..10][-5..-1] #last elements
[*0..10][1..-2]  #middle elements
```

But a negative start with a non-negative end is quite weird. What is that operation even supposed to mean? What is it useful for?

```ruby
[*0..10][-8..8]  #????

8.times{ |i|  p (0..i) => [*0..i][-3..3]  }
{0..0=>nil}
{0..1=>nil}
{0..2=>[0, 1, 2]}
{0..3=>[1, 2, 3]}
{0..4=>[2, 3]}
{0..5=>[3]}
{0..6=>[]}
{0..7=>[]}
```

So even if `[*0..10][-100..100]` remains supported forever (there doesn't seem to be a point in breaking compatibility; see #16822), it could emit a verbose-mode warning.
And ArithmeticSequence slicing should not attempt to be consistent with that case, because it's useless to start with.
So I believe there are two useful/meaningful possibilities for `(0..20).to_a[(-5..5) % 2]`
a) `[16, 18, 20]` ignore trailing non-negative values, like `(-5..) % 2`; I think this makes the most sense
b) `[1, 3, 5]` ignore leading negative values, like python


----------------------------------------
Feature #16812: Allow slicing arrays with ArithmeticSequence
https://bugs.ruby-lang.org/issues/16812#change-86339

* Author: zverok (Victor Shepelev)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:98979] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence
  2020-04-23 15:32 [ruby-core:98034] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence zverok.offline
                   ` (8 preceding siblings ...)
  2020-06-26 19:08 ` [ruby-core:98963] " daniel
@ 2020-06-27  9:14 ` zverok.offline
  2020-07-20  7:04 ` [ruby-core:99235] " matz
  10 siblings, 0 replies; 12+ messages in thread
From: zverok.offline @ 2020-06-27  9:14 UTC (permalink / raw
  To: ruby-core

Issue #16812 has been updated by zverok (Victor Shepelev).


> But a negative start with a non-negative end is quite weird. What is that operation even supposed to mean? What is it useful for?

I believe such edge cases might emerge not being directly written, but when dynamically calculated. Imagine calculating some anchor element, and then taking N elements around it. Then, you have, say...
```ruby
def around_mean(ary, count: 3)
  i = ary.index(ary.sum / ary.length)
  ary[i-count..i+count]
end
around_mean((1..20).to_a)
# => [7, 8, 9, 10, 11, 12, 13] 
around_mean((1..6).to_a)
# => [6] -- hm, it is a bit strange
around_mean((1..6).to_a, count: 10)
# => nil -- hm, it is even weirder...
```
The example before last is `[1, 2, 3, 4, 5, 6][-1..5]` which "intuitively weird", as one _might_ expect something like:
```
  1 2 3 4 5 6
^^^^^^^^^^^
```
The very last example is `[1, 2, 3, 4, 5, 6][-8..12]` -- and it even doesn't produce empty array (I pointed at this at #16822, too).

That's not the best possible example, but at least it demonstrates how we can arrive at edge case situation and why we (probably) might expect different behavior here.

----------------------------------------
Feature #16812: Allow slicing arrays with ArithmeticSequence
https://bugs.ruby-lang.org/issues/16812#change-86359

* Author: zverok (Victor Shepelev)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [ruby-core:99235] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence
  2020-04-23 15:32 [ruby-core:98034] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence zverok.offline
                   ` (9 preceding siblings ...)
  2020-06-27  9:14 ` [ruby-core:98979] " zverok.offline
@ 2020-07-20  7:04 ` matz
  10 siblings, 0 replies; 12+ messages in thread
From: matz @ 2020-07-20  7:04 UTC (permalink / raw
  To: ruby-core

Issue #16812 has been updated by matz (Yukihiro Matsumoto).


The basic bahavior seems OK. Probably we need to investigate some corner cases, but you can commit (and we experiment).

Matz.


----------------------------------------
Feature #16812: Allow slicing arrays with ArithmeticSequence
https://bugs.ruby-lang.org/issues/16812#change-86620

* Author: zverok (Victor Shepelev)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
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/

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2020-07-20  7:04 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-04-23 15:32 [ruby-core:98034] [Ruby master Feature#16812] Allow slicing arrays with ArithmeticSequence zverok.offline
2020-04-23 20:04 ` [ruby-core:98036] " eregontp
2020-04-23 20:44 ` [ruby-core:98037] " zverok.offline
2020-04-24  0:19 ` [ruby-core:98039] " daniel
2020-04-24  4:08 ` [ruby-core:98041] " nobu
2020-04-24  5:09 ` [ruby-core:98042] " muraken
2020-04-25 17:48 ` [ruby-core:98065] " zverok.offline
2020-06-18  2:06 ` [ruby-core:98846] " muraken
2020-06-19 14:12 ` [ruby-core:98886] " muraken
2020-06-26 19:08 ` [ruby-core:98963] " daniel
2020-06-27  9:14 ` [ruby-core:98979] " zverok.offline
2020-07-20  7:04 ` [ruby-core:99235] " matz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).