Project

General

Profile

Actions

Feature #15092

closed

Provide step count in Range constructor

Added by v0dro (Sameer Deshmukh) over 6 years ago. Updated over 6 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:88901]

Description

I would like to propose making changes to the Range constructor so that a user can specify
a step count along with start and stop. Since Ruby 2.6 will introduce a step property
in Ranges anyway I think this will be a useful addition.

Here's my reasons for the changes:

When creating software libraries for numerical computing, it is common to query the data
container for values in a particular range at some given steps. For example, say I have the
following NArray object:

a = NArray.new([1,2,3,4,5,6,7,8,9,10,11,12])

And I want the values 1, 4, 7, 10 and 12, I can simply specify a Range like this:

r = Range.new 0, Float::INFINITY, 3 # start, stop (upto the end), step
a[r]
# => NArray([1, 4, 7, 10, 12])

This can possibly also be extended to Array#[] so that users can get ranges of values at
steps without much worry.


Related issues 1 (0 open1 closed)

Is duplicate of Ruby master - Feature #13904: getter for original information of EnumeratorClosedmrkn (Kenta Murata)Actions

Updated by marcandre (Marc-Andre Lafortune) over 6 years ago

  • Status changed from Open to Feedback

No need to change the constructor. Instead of:

Range.new 0, Float::INFINITY, 3

Use the shorter

(0...) % 3

Updated by v0dro (Sameer Deshmukh) over 6 years ago

Advantages of changing the constructor:

  • Makes it easy to read test code for someone new to Ruby.
  • Consistency in specifying step count in constructor (simple and straightforward way) and using
    the shorter syntax (idiomatic Ruby way).

Disadvantages:

  • Few extra lines of code to change the constructor.

I think the advantages outweigh the disadvantages.

Updated by duerst (Martin Dürst) over 6 years ago

v0dro (Sameer Deshmukh) wrote:

And I want the values 1, 4, 7, 10 and 12, I can simply specify a Range like this:

r = Range.new 0, Float::INFINITY, 3 # start, stop (upto the end), step
a[r]
# => NArray([1, 4, 7, 10, 12])

Wouldn't the result be [1, 4, 7, 10, 13]?

Updated by v0dro (Sameer Deshmukh) over 6 years ago

Wouldn't the result be [1, 4, 7, 10, 13]?

Ah yes. My bad. Editing the description. Thank you.

Updated by v0dro (Sameer Deshmukh) over 6 years ago

v0dro (Sameer Deshmukh) wrote:

I would like to propose making changes to the Range constructor so that a user can specify
a step count along with start and stop. Since Ruby 2.6 will introduce a step property
in Ranges anyway I think this will be a useful addition.

Here's my reasons for the changes:

When creating software libraries for numerical computing, it is common to query the data
container for values in a particular range at some given steps. For example, say I have the
following NArray object:

a = NArray.new([1,2,3,4,5,6,7,8,9,10,11,12])

And I want the values 1, 4, 7, 10 and 12, I can simply specify a Range like this:

r = Range.new 0, Float::INFINITY, 3 # start, stop (upto the end), step
a[r]
# => NArray([1, 4, 7, 10, 12])

This can possibly also be extended to Array#[] so that users can get ranges of values at
steps without much worry.

The array will return [1,4,7,10]. Sorry for previous mistake.

Updated by shevegen (Robert A. Heiler) over 6 years ago

Wouldn't the result be [1, 4, 7, 10, 13]?

Off-by-one is ... common. :)

A bit more on topic, Float::INFINITY is quite long. Could
we not use :infinity to refer to it in some methods or
something shorter? I think ruby users should not need to
have to know the leading "namespace" (Float) in order to
refer to a concept of infinity in ruby.

Updated by marcandre (Marc-Andre Lafortune) over 6 years ago

v0dro (Sameer Deshmukh) wrote:

Advantages of changing the constructor:

  • Makes it easy to read test code for someone new to Ruby.

Very debatable.

  • Consistency in specifying step count in constructor (simple and straightforward way) and using
    the shorter syntax (idiomatic Ruby way).

It's actually not consistent, since the result is a Enumerator::ArithmeticSequence, not a Range.

Disadvantages:

  • Few extra lines of code to change the constructor.

I realize you wrote "introduces a step property in Ranges anyway I think this will be a useful addition", but this is not true. Range will not have a step property. It's simply that the step method will now return a different class of object that is more useful.

Actions #8

Updated by mrkn (Kenta Murata) over 6 years ago

  • Is duplicate of Feature #13904: getter for original information of Enumerator added

Updated by mrkn (Kenta Murata) over 6 years ago

We've already have Emuerator::ArithmeticSequence in trunk.
Please use it.

Updated by v0dro (Sameer Deshmukh) over 6 years ago

OK I'm convinced this is probably a bad idea since step returns an ArithmeticSequence
and is not a property of Range.

Closing the issue.

Actions #11

Updated by mrkn (Kenta Murata) over 6 years ago

  • Status changed from Feedback to Rejected
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0