[#106939] [Ruby master Bug#18455] `IO#close` has poor performance and difficult to understand semantics. — "ioquatix (Samuel Williams)" <noreply@...>

Issue #18455 has been reported by ioquatix (Samuel Williams).

10 messages 2022/01/01

[#106977] [Ruby master Feature#18461] closures are capturing unused variables — "bughit (bug hit)" <noreply@...>

Issue #18461 has been reported by bughit (bug hit).

12 messages 2022/01/05

[#106994] [Ruby master Feature#18462] Proposal to merge WASI based WebAssembly support — "katei (Yuta Saito)" <noreply@...>

Issue #18462 has been reported by katei (Yuta Saito).

8 messages 2022/01/07

[#106996] [Ruby master Feature#18463] Random number generation with xoshiro — "bbrklm (Benson Muite)" <noreply@...>

Issue #18463 has been reported by bbrklm (Benson Muite).

8 messages 2022/01/07

[#107005] [Ruby master Bug#18464] RUBY_INTERNAL_EVENT_NEWOBJ tracepoint causes an interpreter crash when combined with Ractors — "kjtsanaktsidis (KJ Tsanaktsidis)" <noreply@...>

Issue #18464 has been reported by kjtsanaktsidis (KJ Tsanaktsidis).

14 messages 2022/01/08

[#107008] [Ruby master Bug#18465] Make `IO#write` atomic. — "ioquatix (Samuel Williams)" <noreply@...>

Issue #18465 has been reported by ioquatix (Samuel Williams).

16 messages 2022/01/09

[#107073] [Ruby master Feature#18481] Porting YJIT to Rust (request for feedback) — "maximecb (Maxime Chevalier-Boisvert)" <noreply@...>

Issue #18481 has been reported by maximecb (Maxime Chevalier-Boisvert).

26 messages 2022/01/12

[#107106] [Ruby master Bug#18487] Kernel#binding behaves differently depending on implementation language of items on the stack — "alanwu (Alan Wu)" <noreply@...>

Issue #18487 has been reported by alanwu (Alan Wu).

11 messages 2022/01/13

[#107190] [Ruby master Feature#18498] Introduce a public WeakKeysMap that compares by equality — "byroot (Jean Boussier)" <noreply@...>

Issue #18498 has been reported by byroot (Jean Boussier).

17 messages 2022/01/19

[#107203] [Ruby master Bug#18501] [BUG] try to mark T_NONE object in RubyVM::InstructionSequence. load_from_binary — "byroot (Jean Boussier)" <noreply@...>

Issue #18501 has been reported by byroot (Jean Boussier).

8 messages 2022/01/20

[#107204] [Ruby master Bug#18502] Make ruby-2.7.5 on Solaris 10 ld.so.1: gcc: fatal: libintl.so.8: open failed: No such file or directory — "dklein (Dmitri Klein)" <noreply@...>

Issue #18502 has been reported by dklein (Dmitri Klein).

8 messages 2022/01/20

[#107275] [Ruby master Bug#18512] MacOS 12.1 Monterey Bug — "oucl5976@... (Paul Liu)" <noreply@...>

Issue #18512 has been reported by oucl5976@gmail.com (Paul Liu).

9 messages 2022/01/25

[#107291] [Ruby master Bug#18518] NoMemoryError + [FATAL] failed to allocate memory for twice 1 << large — "Eregon (Benoit Daloze)" <noreply@...>

Issue #18518 has been reported by Eregon (Benoit Daloze).

12 messages 2022/01/26

[#107310] [Ruby master Bug#18555] Running "bundle exec middleman server" on M1 Mac gives [BUG] Bus Error at 0x0000000104b04000 — "anthonyaykut (Anthony Aykut)" <noreply@...>

Issue #18555 has been reported by anthonyaykut (Anthony Aykut).

13 messages 2022/01/28

[#107346] [Ruby master Misc#18557] DevMeeting-2022-02-17 — "mame (Yusuke Endoh)" <noreply@...>

Issue #18557 has been reported by mame (Yusuke Endoh).

18 messages 2022/01/29

[#107392] [Ruby master Bug#18560] "Compaction isn't available on this platform" error running PG test suite on ppc64le — "vo.x (Vit Ondruch)" <noreply@...>

Issue #18560 has been reported by vo.x (Vit Ondruch).

7 messages 2022/01/31

[ruby-core:107239] [Ruby master Feature#16757] Add intersection to Range

From: "baweaver (Brandon Weaver)" <noreply@...>
Date: 2022-01-22 07:26:54 UTC
List: ruby-core #107239
Issue #16757 has been updated by baweaver (Brandon Weaver).


I'd like to see this explored, as I find myself solving a number of problems that involve Range merging, and potential intersections.

My current implementation, in Ruby, would be something to the note of:

```ruby
class Range
  # To do this we would have to either inline this overlap checking
  # code in the methods, or introduce overlap checks for ranges
  # beyond single values as `cover?` does.

  # If other Range begins inside of the current Range
  #   (0..5).left_overlap?(3..7) # true
  def left_overlap?(other)
    cover?(other.begin)
  end

  # If self Range begins inside the other Range
  #   (3..7).left_overlap?(0..5) # true
  def right_overlap?(other)
    other.cover?(self.begin)
  end

  # If the current Range overlaps the other
  def overlap?(other)
    left_overlap?(other) || right_overlap?(other)
  end

  def join(other)
    return self.class.new(self.begin, other.end) if left_overlap?(other)
    return self.class.new(other.begin, self.end) if right_overlap?(other)

    # This is contentious if there is no overlap. Do we return `self`,
    # an empty Array, nil (breaks chaining), or exception (breaks a lot more)
    self
  end
  # Though I really hesitate to add `+` here in case it fails from
  # no overlaps
  alias_method :+, :join

  def intersection(other)
    return self.class.new(other.begin, self.end) if left_overlap?(other)
    return self.class.new(self.begin, other.end) if right_overlap?(other)

    # This one I feel better about returning an empty Array for, but
    # it does break some type integrity, so not sure what would be
    # more correct here.
    []
  end
  # I do feel better about this
  alias_method :&, :intersection
end
```

There are a few concerns I can see with these implementations which would need to be addressed for consistency and correctness, there may be some I miss.

First is what to do when the origin Range is "greater" than the other Range (`10..20` against `0..5` versus the reverse), would it be ignored as with `(10..1).to_a`? I could see a few different cases for this.

Second is how to handle beginless and endless ranges, though `cover?` prevents issues with `nil?` as is the case with `>=` and other ordering comparators.

Third is how to approach exclusive versus inclusive ranges, which may be something like `self.class.new(other.begin, self.end, self.exclude_end?)` and the reverse in the other case.

In general I am a fan of type-specific methods rather than converting to Array, as is the case with Hash specific methods like `select` returning `Hash` rather than requiring `to_h`.

----------------------------------------
Feature #16757: Add intersection to Range
https://bugs.ruby-lang.org/issues/16757#change-96098

* Author: stuyam (Stuart Yamartino)
* Status: Open
* Priority: Normal
----------------------------------------
It would be great if there was a way to do an intersection with ranges. I wrote a method recently to solve this but it gets complicated and doesn't solve for all edge cases.
The example I was using it for was getting the intersection of two date ranges. I was using it to calculate pricing discounts for overlapping dates for a reservation system.

I would propose something like:
``` ruby
(Date.new(2020, 04, 03)..Date.new(2020, 04, 20)) & (Date.new(2020, 04, 15)..Date.new(2020, 04, 30))

=> Wed, 15 Apr 2020..Mon, 20 Apr 2020
```

There are a handful of array methods that you can just convert a range to and do on an arrays rather than the ranges themselves, like so:
``` ruby
(Date.new(2020, 04, 03)..Date.new(2020, 04, 20)).to_a & (Date.new(2020, 04, 15)..Date.new(2020, 04, 30)).to_a

=> [Wed, 15 Apr 2020, Thu, 16 Apr 2020, Fri, 17 Apr 2020, Sat, 18 Apr 2020, Sun, 19 Apr 2020, Mon, 20 Apr 2020]
```

There are a few issues with this however:
1. Performance: creating the ranges into arrays and doing array calculations can me slower based on the span of the range.
2. Returning a range: if you want to go back to a range, you need to convert the resulting array back to a range, which involves min and maxing the array and handling nil edge cases.
3. Infinite ranges: Now that there are endless and beginless ranges added in ruby 2.6 and 2.7 respectively, you can not convert an infinite range to an array and therefore can't do an intersection without more complex logic.

I think the added infinite range support makes it a good reason to add range intersections since that is a feature you can't accomplish with just an array.

It looks like there was an [Array#intersection alias](https://bugs.ruby-lang.org/issues/16155) added for Array#& recently. I would propose Range#& and Range#intersection to have parity with Array and Set methods.



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

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next