Forum: Ruby-core [ruby-trunk - Feature #7580][Open] Range translation

Posted by charliesome (Charlie Somerville) (Guest)
on 2012-12-17 06:43
(Received via mailing list)
Issue #7580 has been reported by charliesome (Charlie Somerville).

----------------------------------------
Feature #7580: Range translation
https://bugs.ruby-lang.org/issues/7580

Author: charliesome (Charlie Somerville)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin
I would like to propose the (({#+})) and (({#-})) methods on 
(({Range})).

These would be useful for translating ranges - for example, given a 
range where the endpoints are 1-indexed, the range could be translated 
by 1 in the negative direction to use in (({Array#[]})).

Instead of doing a syntactically-bulky manual translation like so:

  ary[(range.begin - 1)..(range.end - 1)]

(({Range#-})) could be used instead:

  ary[range - 1]

The translation methods would not handle certain endpoint types 
specially, they would just pass the call on.

Here's an example implementation in Ruby:

  class Range
    def +(other)
      Range.new(self.begin + other, self.end + other, exclude_end?)
    end

    def -(other)
      Range.new(self.begin - other, self.end - other, exclude_end?)
    end
  end

=end
Posted by naruse (Yui NARUSE) (Guest)
on 2012-12-17 06:57
(Received via mailing list)
Issue #7580 has been updated by naruse (Yui NARUSE).


I think such arithmetic is not addition/subtraction, but shift.
----------------------------------------
Feature #7580: Range translation
https://bugs.ruby-lang.org/issues/7580#change-34791

Author: charliesome (Charlie Somerville)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin
I would like to propose the (({#+})) and (({#-})) methods on 
(({Range})).

These would be useful for translating ranges - for example, given a 
range where the endpoints are 1-indexed, the range could be translated 
by 1 in the negative direction to use in (({Array#[]})).

Instead of doing a syntactically-bulky manual translation like so:

  ary[(range.begin - 1)..(range.end - 1)]

(({Range#-})) could be used instead:

  ary[range - 1]

The translation methods would not handle certain endpoint types 
specially, they would just pass the call on.

Here's an example implementation in Ruby:

  class Range
    def +(other)
      Range.new(self.begin + other, self.end + other, exclude_end?)
    end

    def -(other)
      Range.new(self.begin - other, self.end - other, exclude_end?)
    end
  end

=end
Posted by dummey (Ricky Ng) (Guest)
on 2012-12-17 07:03
(Received via mailing list)
Issue #7580 has been updated by dummey (Ricky Ng).


I would normally agree that 'shift' would be the proper term except that 
it's used in Array already which could cause a bit of confusion.
----------------------------------------
Feature #7580: Range translation
https://bugs.ruby-lang.org/issues/7580#change-34793

Author: charliesome (Charlie Somerville)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin
I would like to propose the (({#+})) and (({#-})) methods on 
(({Range})).

These would be useful for translating ranges - for example, given a 
range where the endpoints are 1-indexed, the range could be translated 
by 1 in the negative direction to use in (({Array#[]})).

Instead of doing a syntactically-bulky manual translation like so:

  ary[(range.begin - 1)..(range.end - 1)]

(({Range#-})) could be used instead:

  ary[range - 1]

The translation methods would not handle certain endpoint types 
specially, they would just pass the call on.

Here's an example implementation in Ruby:

  class Range
    def +(other)
      Range.new(self.begin + other, self.end + other, exclude_end?)
    end

    def -(other)
      Range.new(self.begin - other, self.end - other, exclude_end?)
    end
  end

=end
Posted by charliesome (Charlie Somerville) (Guest)
on 2012-12-17 07:03
(Received via mailing list)
Issue #7580 has been updated by charliesome (Charlie Somerville).


=begin
Do you propose that (({Range#<<})) would use (({#-})) and (({Range#>>})) 
would use (({#+})), or would it be a different method call internally?

I am happy with both alternatives, I just want nice convenience methods 
for this operation.
=end
----------------------------------------
Feature #7580: Range translation
https://bugs.ruby-lang.org/issues/7580#change-34792

Author: charliesome (Charlie Somerville)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin
I would like to propose the (({#+})) and (({#-})) methods on 
(({Range})).

These would be useful for translating ranges - for example, given a 
range where the endpoints are 1-indexed, the range could be translated 
by 1 in the negative direction to use in (({Array#[]})).

Instead of doing a syntactically-bulky manual translation like so:

  ary[(range.begin - 1)..(range.end - 1)]

(({Range#-})) could be used instead:

  ary[range - 1]

The translation methods would not handle certain endpoint types 
specially, they would just pass the call on.

Here's an example implementation in Ruby:

  class Range
    def +(other)
      Range.new(self.begin + other, self.end + other, exclude_end?)
    end

    def -(other)
      Range.new(self.begin - other, self.end - other, exclude_end?)
    end
  end

=end
Posted by naruse (Yui NARUSE) (Guest)
on 2012-12-17 08:14
(Received via mailing list)
Issue #7580 has been updated by naruse (Yui NARUSE).

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

charliesome (Charlie Somerville) wrote:
> Do you propose that (({Range#<<})) would use (({#-})) and (({Range#>>})) would 
use (({#+})), or would it be a different method call internally?

Don't use +/- and use <</>> or Range#shift().

> I am happy with both alternatives, I just want nice convenience methods for this 
operation.

In my experience, such alternative name considered harmful
because if you want to add another method as Arange#+ in the future, 
those aliases prevent it.
----------------------------------------
Feature #7580: Range translation
https://bugs.ruby-lang.org/issues/7580#change-34795

Author: charliesome (Charlie Somerville)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category:
Target version:


=begin
I would like to propose the (({#+})) and (({#-})) methods on 
(({Range})).

These would be useful for translating ranges - for example, given a 
range where the endpoints are 1-indexed, the range could be translated 
by 1 in the negative direction to use in (({Array#[]})).

Instead of doing a syntactically-bulky manual translation like so:

  ary[(range.begin - 1)..(range.end - 1)]

(({Range#-})) could be used instead:

  ary[range - 1]

The translation methods would not handle certain endpoint types 
specially, they would just pass the call on.

Here's an example implementation in Ruby:

  class Range
    def +(other)
      Range.new(self.begin + other, self.end + other, exclude_end?)
    end

    def -(other)
      Range.new(self.begin - other, self.end - other, exclude_end?)
    end
  end

=end
Posted by charliesome (Charlie Somerville) (Guest)
on 2013-01-25 15:15
(Received via mailing list)
Issue #7580 has been updated by charliesome (Charlie Somerville).

Target version set to next minor


----------------------------------------
Feature #7580: Range translation
https://bugs.ruby-lang.org/issues/7580#change-35634

Author: charliesome (Charlie Somerville)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category:
Target version: next minor


=begin
I would like to propose the (({#+})) and (({#-})) methods on 
(({Range})).

These would be useful for translating ranges - for example, given a 
range where the endpoints are 1-indexed, the range could be translated 
by 1 in the negative direction to use in (({Array#[]})).

Instead of doing a syntactically-bulky manual translation like so:

  ary[(range.begin - 1)..(range.end - 1)]

(({Range#-})) could be used instead:

  ary[range - 1]

The translation methods would not handle certain endpoint types 
specially, they would just pass the call on.

Here's an example implementation in Ruby:

  class Range
    def +(other)
      Range.new(self.begin + other, self.end + other, exclude_end?)
    end

    def -(other)
      Range.new(self.begin - other, self.end - other, exclude_end?)
    end
  end

=end
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.