From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-3.2 required=3.0 tests=AWL,BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.1 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 429911F403 for ; Fri, 15 Jun 2018 17:14:53 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 956BE120B1E; Sat, 16 Jun 2018 02:14:49 +0900 (JST) Received: from o1678916x28.outbound-mail.sendgrid.net (o1678916x28.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 91734120B0E for ; Sat, 16 Jun 2018 02:14:46 +0900 (JST) Received: by filter0051p3las1.sendgrid.net with SMTP id filter0051p3las1-21541-5B23F403-4 2018-06-15 17:14:43.155382724 +0000 UTC Received: from herokuapp.com (ec2-54-163-108-56.compute-1.amazonaws.com [54.163.108.56]) by ismtpd0023p1iad2.sendgrid.net (SG) with ESMTP id gqPKmrUCT9SYvwORjaUlgQ Fri, 15 Jun 2018 17:14:43.026 +0000 (UTC) Date: Fri, 15 Jun 2018 17:14:43 +0000 (UTC) From: eregontp@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 62886 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 14799 X-Redmine-Issue-Author: zverok X-Redmine-Issue-Assignee: matz X-Redmine-Sender: Eregon X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS7CJtyYvN4RXSMt1ilzazdFQrMzWqQHmhy7k3 cnigO+O4Sn41pTYICiqLcLgdsA5yoSVf9G72OxV9EbUzd57Qp5LD+6N4MX7Iy1vWH1wf/I0PNrNHnZ JylpLzaruZZPmYDv27XOL9lWFCErtfMKC1kflSpEidNujc3opECN8357Pw== X-ML-Name: ruby-core X-Mail-Count: 87500 Subject: [ruby-core:87500] [Ruby trunk Feature#14799] Startless range X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #14799 has been updated by Eregon (Benoit Daloze). @mame Ah, that is quite unfortunate (it does not look so nice with the parens), but thank you for mentioning the caveat. I guess much like Range literals in general, it's often needed to have parens around them. ---------------------------------------- Feature #14799: Startless range https://bugs.ruby-lang.org/issues/14799#change-72506 * 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/