ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: mame@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:87318] [Ruby trunk Feature#14799][Assigned] Startless range
Date: Thu, 31 May 2018 11:39:02 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-72311.20180531113900.d271f2355628f295@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-14799.20180531075634@ruby-lang.org

Issue #14799 has been updated by mame (Yusuke Endoh).

File beginless-range.patch added
Status changed from Open to Assigned
Assignee set to matz (Yukihiro Matsumoto)

I tried begin-less range once, and it caused many parser conflicts, so I gave up.

However, I've tried it again and created a patch with no conflicts.
A proof-of-concept patch is attached.  It uses the damn lexer state, so I'd like nobu and yui-knk to review it.
Note that the main focus of this patch is parse.y, so we need more work to let many builtin methods to support begin-less range.
 
Matz, what do you think?

----------------------------------------
Feature #14799: Startless range
https://bugs.ruby-lang.org/issues/14799#change-72311

* Author: zverok (Victor Shepelev)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
On introduction of endless range at #12912, "startless range" was discussed this way:

> @sowieso: Not having the opposite (`..5` and `..-2`) feels like this is rather a hack than a thoroughly planned feature.

> @duerst: I don't understand the need for a `..5` Range. The feature is called "endless range". Although mathematically, it's possible to think about startless ranges, they don't work in a program. Maybe some programming languages have `..5` as a shortcut for `0..5`, but that's in any way a usual, bounded, range with a start and an end. It's conceptually totally different from `5..`, which is a range with a start but no end, an unbound range.

In the context of that ticket (ranges used mostly for slicing arrays) having `..5` was indeed hard to justify, but there are other cases when `..5` being `-Infinity..5` is absolutely reasonable:

```ruby
case release_date
when ..1.year.ago 
  puts "ancient"
when 1.year.ago..3.months.ago
  puts "old"
when 3.months.ago..Date.today
  puts "recent"
when Date.today..
  puts "upcoming"
end

log.map(&:logged_at).grep(..Date.new(1980)) # => outliers due to bad log parsing...
```

E.g., whenever case equality operator is acting, having startless range to express "below this value" is the most concise and readable way. Also, for expressing constants (mostly decorative, but very readable):

```ruby
# Celsius degrees
WORK_RANGES = {
   ..-10 => :turn_off,
  -10..0 => :energy_saving,
  0..20 => :main,
  20..35 => :cooling,
  35.. => :turn_off
}
```

In addition, my related proposal #14784 suggests that this kind of ranges could be utilized by more powerful clamp too:

```ruby
updated_at.clamp(..Date.today)
```

**Uncertainty points:**

* Would it be hard to add to parser? I am not sure, I am not very good at it :(
* Should `..` be a thing? I guess not, unless there would be convincing real-life examples, which for me it is hard to think of.

---Files--------------------------------
beginless-range.patch (3.55 KB)


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

  parent reply	other threads:[~2018-05-31 11:39 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-14799.20180531075634@ruby-lang.org>
2018-05-31  7:56 ` [ruby-core:87313] [Ruby trunk Feature#14799] Startless range zverok.offline
2018-05-31  8:10 ` [ruby-core:87314] " shyouhei
2018-05-31  9:14 ` [ruby-core:87315] " zverok.offline
2018-05-31  9:31 ` [ruby-core:87316] " shyouhei
2018-05-31  9:51 ` [ruby-core:87317] " zverok.offline
2018-05-31 11:39 ` mame [this message]
2018-05-31 13:01 ` [ruby-core:87320] " janosch84
2018-06-04 11:10 ` [ruby-core:87388] " darkwiiplayer
2018-06-14 14:34 ` [ruby-core:87492] " zn
2018-06-14 15:16 ` [ruby-core:87494] " eregontp
2018-06-14 22:30 ` [ruby-core:87496] " mame
2018-06-15 17:14 ` [ruby-core:87500] " eregontp
2018-06-21  9:02 ` [ruby-core:87561] " knu
2019-03-11  5:17 ` [ruby-core:91747] " matz
2019-03-11  5:29 ` [ruby-core:91749] " nobu
2019-04-03  8:16 ` [ruby-core:92123] " 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-72311.20180531113900.d271f2355628f295@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).