ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: manga.osyo@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:92752] [Ruby trunk Feature#15864] Proposal: Add methods to determine if it is an infinite range
Date: Wed, 22 May 2019 01:22:23 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-78114.20190522012222.f1cc553735034f5e@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15864.20190521014637@ruby-lang.org

Issue #15864 has been updated by osyo (manga osyo).


How about doing this?

```ruby
("a".."z").size # => nil
(.."z").size    # => Infinity
("a"..).size    # => Infinity
```


----------------------------------------
Feature #15864: Proposal: Add methods to determine if it is an infinite range
https://bugs.ruby-lang.org/issues/15864#change-78114

* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------

## Summary

Proposal to add methods to determine if it is an infinite range.


## Current status

```ruby
# begin / end return nil
p (..10).begin  # => nil
p (1..).end     # => nil

# But, first / last raise an exception
p (..10).first  # Error: in `first': cannot get the first element of beginless range (RangeError)
p (1..).last    # Error: in `last': cannot get the last element of endless range (RangeError)

# Returns Infinity if it is a Numeric
p (1..10).size      # => 10
p (1..).size        # => Infinity
p (..1).size        # => Infinity

# Otherwise returns nil
p ("a".."z").size   # => nil
p ("a"..).size      # => nil
p (.."z").size      # => nil
```

* `(..10).begin` return `nil`, but `(..10).first` raise an exception.
* `(1..).size` return `Infinity`, but `("a"..).size` return `nil`.
* Behavior changes depending on the state of Range and the called method.
* It is difficult to determine if it is an infinite range.


## Proposal methods

* `Range#beginless?`
  * return `true` if `begin` is `nil`
* `Range#endless?`
  * return `true` if `end` is `nil`
* `Range#infinite?`
  * return `true` if `begin` is `nil` or `end` is `nil`
* `Range#finite?`
  * return `true` if `begin` is not `nil` and `end` is not `nil`


## Example

```ruby
p (1..10).beginless?  # => false
p (1..10).endless?    # => false
p (1..10).infinite?   # => false
p (1..10).finite?     # => true

p (..10).beginless?   # => true
p (..10).endless?     # => false
p (..10).infinite?    # => true
p (..10).finite?      # => false

p (1..).beginless?    # => false
p (1..).endless?      # => true
p (1..).infinite?     # => true
p (1..).finite?       # => false

# NOTE: Float::INFINITY is not support
p (1..Float::INFINITY).beginless?    # => false
p (1..Float::INFINITY).endless?      # => false
p (1..Float::INFINITY).infinite?     # => false
p (1..Float::INFINITY).finite?       # => true
```


## Use case

### before

```ruby
def search_in(range)
  query = "/search"
  if !(range.begin.nil? || range.end.nil?)
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.begin.nil?
    "#{query}?to=#{range.end}"
  elsif range.end.nil?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```

### after

```ruby
def search_in(range)
  query = "/search"
  if range.finite?
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.beginless?
    "#{query}?to=#{range.end}"
  elsif range.endless?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```


## Memo

* Check whether the tip is infinite only with `nil`.
* `Float::INFINITY` is not supported.
  * I think that there is no relation between `Float::INFINITY` and infinite Range.
* I would like an opinion on how to determine if it is infinite.
  * see `range.begin.infinite?` ?
* Uhether there is a better name for the method name.
  * e.g. `#infinite?` to `#infinity?`.


Thank you.

github pull request: https://github.com/ruby/ruby/pull/2196




-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2019-05-22  1:22 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15864.20190521014637@ruby-lang.org>
2019-05-21  1:46 ` [ruby-core:92732] [Ruby trunk Feature#15864] Proposal: Add methods to determine if it is an infinite range manga.osyo
2019-05-21  8:16 ` [ruby-core:92738] " shevegen
2019-05-21 18:58 ` [ruby-core:92748] " eregontp
2019-05-21 23:13 ` [ruby-core:92750] " mame
2019-05-22  1:14 ` [ruby-core:92751] " manga.osyo
2019-05-22  1:22 ` manga.osyo [this message]
2019-05-22  2:54 ` [ruby-core:92754] " mame
2019-05-22  8:56 ` [ruby-core:92776] " matz
2019-08-22  0:07 ` [ruby-core:94468] [Ruby master " muraken
2019-08-22  3:23 ` [ruby-core:94471] " sawadatsuyoshi
2019-08-22  7:36 ` [ruby-core:94475] " mame

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-78114.20190522012222.f1cc553735034f5e@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).