[ruby-core:127414] [Ruby Feature#22536] Add Enumerable#longest_slice to return the range of the longest consecutive matching sequence
Issue #22536 has been reported by datibbaw (Tjerk Meesters). ---------------------------------------- Feature #22536: Add Enumerable#longest_slice to return the range of the longest consecutive matching sequence https://bugs.ruby-lang.org/issues/22536 * Author: datibbaw (Tjerk Meesters) * Status: Open ---------------------------------------- I propose adding `Enumerable#longest_slice`, which returns a Range identifying the longest consecutive sequence of matching entries. Without a block, entries match when they are truthy. Both `nil` and `false` break a sequence. With a block, entries match when the block returns a truthy value. ### Motivation Finding the longest consecutive matching sequence currently requires manually tracking positions and run lengths, or creating intermediate collections. Returning a Range makes the result directly usable for slicing an Array or identifying positions in another enumerable. I ran into this when analysing IPv6 compression logic and reasoned that it may have other uses; in comparison, longest prefix / suffix is already covered with e.g. `#index` and `#rindex` using block syntax. ### Examples [nil, 'a', 'b', nil, 'c'].longest_slice # => 1...3 [1, 2, 4, 3, 6].longest_slice(&:even?) # => 1...3 [false, 0, []].longest_slice # => 1...3 a = [10, 20, 30, 40] a.each.with_index(1).longest_slice do |value, index| value >= 20 && index <= 3 end # => 1...3 ### Proposed behavior * Return a zero-based, end-exclusive Range. * Return `0...0` for an empty enumerable or when no entries match. * When multiple matching sequences have the same maximum length, return the first. * Forward all arguments yielded by `each` to the block, supporting indexed enumerators and multi-argument iterators. * Always express the result in iteration positions, independently of any index yielded to the block. * Traverse the enumerable once, using constant auxiliary space. The name `longest_slice` and the API details are open for discussion. A draft implementation with documentation and tests is available at: https://github.com/ruby/ruby/pull/19838 -- https://bugs.ruby-lang.org/
Issue #22536 has been updated by duerst (Martin Dürst). My impression is that this proposal is useful in some cases, but these are too rare to include it in core. This functionality is better suited for a gem. Maybe it's already available in some gem. ---------------------------------------- Feature #22536: Add Enumerable#longest_slice to return the range of the longest consecutive matching sequence https://bugs.ruby-lang.org/issues/22536#change-119878 * Author: datibbaw (Tjerk Meesters) * Status: Open ---------------------------------------- I propose adding `Enumerable#longest_slice`, which returns a Range identifying the longest consecutive sequence of matching entries. Without a block, entries match when they are truthy. Both `nil` and `false` break a sequence. With a block, entries match when the block returns a truthy value. ### Motivation Finding the longest consecutive matching sequence currently requires manually tracking positions and run lengths, or creating intermediate collections. Returning a Range makes the result directly usable for slicing an Array or identifying positions in another enumerable. I ran into this when analysing IPv6 compression logic and reasoned that it may have other uses; in comparison, longest prefix / suffix is already covered with e.g. `#index` and `#rindex` using block syntax. ### Examples [nil, 'a', 'b', nil, 'c'].longest_slice # => 1...3 [1, 2, 4, 3, 6].longest_slice(&:even?) # => 1...3 [false, 0, []].longest_slice # => 1...3 a = [10, 20, 30, 40] a.each.with_index(1).longest_slice do |value, index| value >= 20 && index <= 3 end # => 1...3 ### Proposed behavior * Return a zero-based, end-exclusive Range. * Return `0...0` for an empty enumerable or when no entries match. * When multiple matching sequences have the same maximum length, return the first. * Forward all arguments yielded by `each` to the block, supporting indexed enumerators and multi-argument iterators. * Always express the result in iteration positions, independently of any index yielded to the block. * Traverse the enumerable once, using constant auxiliary space. The name `longest_slice` and the API details are open for discussion. A draft implementation with documentation and tests is available at: https://github.com/ruby/ruby/pull/19838 -- https://bugs.ruby-lang.org/
Issue #22536 has been updated by datibbaw (Tjerk Meesters). duerst (Martin Dürst) wrote in #note-1:
My impression is that this proposal is useful in some cases, but these are too rare to include it in core. This functionality is better suited for a gem. Maybe it's already available in some gem.
I did search around for gems that come close; `rubyworks/facets` has `Enumerable#clusters` which is similar to `slice_when` and returns an array of arrays, or `Array#span` which is just a wrapper over `find_index` and `take/drop`. It's certainly possible to pour this idea into a gem, along the lines of operations that return "pointers" into an array rather than a full copy. ---------------------------------------- Feature #22536: Add Enumerable#longest_slice to return the range of the longest consecutive matching sequence https://bugs.ruby-lang.org/issues/22536#change-119896 * Author: datibbaw (Tjerk Meesters) * Status: Open ---------------------------------------- I propose adding `Enumerable#longest_slice`, which returns a Range identifying the longest consecutive sequence of matching entries. Without a block, entries match when they are truthy. Both `nil` and `false` break a sequence. With a block, entries match when the block returns a truthy value. ### Motivation Finding the longest consecutive matching sequence currently requires manually tracking positions and run lengths, or creating intermediate collections. Returning a Range makes the result directly usable for slicing an Array or identifying positions in another enumerable. I ran into this when analysing IPv6 compression logic and reasoned that it may have other uses; in comparison, longest prefix / suffix is already covered with e.g. `#index` and `#rindex` using block syntax. ### Examples [nil, 'a', 'b', nil, 'c'].longest_slice # => 1...3 [1, 2, 4, 3, 6].longest_slice(&:even?) # => 1...3 [false, 0, []].longest_slice # => 1...3 a = [10, 20, 30, 40] a.each.with_index(1).longest_slice do |value, index| value >= 20 && index <= 3 end # => 1...3 ### Proposed behavior * Return a zero-based, end-exclusive Range. * Return `0...0` for an empty enumerable or when no entries match. * When multiple matching sequences have the same maximum length, return the first. * Forward all arguments yielded by `each` to the block, supporting indexed enumerators and multi-argument iterators. * Always express the result in iteration positions, independently of any index yielded to the block. * Traverse the enumerable once, using constant auxiliary space. The name `longest_slice` and the API details are open for discussion. A draft implementation with documentation and tests is available at: https://github.com/ruby/ruby/pull/19838 -- https://bugs.ruby-lang.org/
Issue #22536 has been updated by ufuk (Ufuk Kayserilioglu). One concern that I have about this proposal is that the method proposed needs to iterate all the items to be sure that it found the longest slice, but an `Enumerable` can be infinite, which would guarantee that the method call would never return: ```ruby (1..).longest_slice(&:even) # does this ever return? ``` I know there are other instances of `Enumerable` methods that are misbehaved with infinite items, and if I recall correctly that was a discussion about them on this bug tracker before. The ship has sailed on those ones, but I'd rather not add another misbehaved method on `Enumerable`. The method might be more appropriate on `Array`, though, since all the examples are either on arrays, or ones that can be trivially converted to arrays. ---------------------------------------- Feature #22536: Add Enumerable#longest_slice to return the range of the longest consecutive matching sequence https://bugs.ruby-lang.org/issues/22536#change-119940 * Author: datibbaw (Tjerk Meesters) * Status: Open ---------------------------------------- I propose adding `Enumerable#longest_slice`, which returns a Range identifying the longest consecutive sequence of matching entries. Without a block, entries match when they are truthy. Both `nil` and `false` break a sequence. With a block, entries match when the block returns a truthy value. ### Motivation Finding the longest consecutive matching sequence currently requires manually tracking positions and run lengths, or creating intermediate collections. Returning a Range makes the result directly usable for slicing an Array or identifying positions in another enumerable. I ran into this when analysing IPv6 compression logic and reasoned that it may have other uses; in comparison, longest prefix / suffix is already covered with e.g. `#index` and `#rindex` using block syntax. ### Examples [nil, 'a', 'b', nil, 'c'].longest_slice # => 1...3 [1, 2, 4, 3, 6].longest_slice(&:even?) # => 1...3 [false, 0, []].longest_slice # => 1...3 a = [10, 20, 30, 40] a.each.with_index(1).longest_slice do |value, index| value >= 20 && index <= 3 end # => 1...3 ### Proposed behavior * Return a zero-based, end-exclusive Range. * Return `0...0` for an empty enumerable or when no entries match. * When multiple matching sequences have the same maximum length, return the first. * Forward all arguments yielded by `each` to the block, supporting indexed enumerators and multi-argument iterators. * Always express the result in iteration positions, independently of any index yielded to the block. * Traverse the enumerable once, using constant auxiliary space. The name `longest_slice` and the API details are open for discussion. A draft implementation with documentation and tests is available at: https://github.com/ruby/ruby/pull/19838 -- https://bugs.ruby-lang.org/
Issue #22536 has been updated by duerst (Martin Dürst). datibbaw (Tjerk Meesters) wrote in #note-2:
I did search around for gems that come close; `rubyworks/facets` has `Enumerable#clusters` which is similar to `slice_when` and returns an array of arrays, or `Array#span` which is just a wrapper over `find_index` and `take/drop`.
It's certainly possible to pour this idea into a gem, along the lines of operations that return "pointers" into an array rather than a full copy.
I was thinking of adding this to an existing gem (e.g. `facets`), rather than creating a new one. ---------------------------------------- Feature #22536: Add Enumerable#longest_slice to return the range of the longest consecutive matching sequence https://bugs.ruby-lang.org/issues/22536#change-119966 * Author: datibbaw (Tjerk Meesters) * Status: Open ---------------------------------------- I propose adding `Enumerable#longest_slice`, which returns a Range identifying the longest consecutive sequence of matching entries. Without a block, entries match when they are truthy. Both `nil` and `false` break a sequence. With a block, entries match when the block returns a truthy value. ### Motivation Finding the longest consecutive matching sequence currently requires manually tracking positions and run lengths, or creating intermediate collections. Returning a Range makes the result directly usable for slicing an Array or identifying positions in another enumerable. I ran into this when analysing IPv6 compression logic and reasoned that it may have other uses; in comparison, longest prefix / suffix is already covered with e.g. `#index` and `#rindex` using block syntax. ### Examples [nil, 'a', 'b', nil, 'c'].longest_slice # => 1...3 [1, 2, 4, 3, 6].longest_slice(&:even?) # => 1...3 [false, 0, []].longest_slice # => 1...3 a = [10, 20, 30, 40] a.each.with_index(1).longest_slice do |value, index| value >= 20 && index <= 3 end # => 1...3 ### Proposed behavior * Return a zero-based, end-exclusive Range. * Return `0...0` for an empty enumerable or when no entries match. * When multiple matching sequences have the same maximum length, return the first. * Forward all arguments yielded by `each` to the block, supporting indexed enumerators and multi-argument iterators. * Always express the result in iteration positions, independently of any index yielded to the block. * Traverse the enumerable once, using constant auxiliary space. The name `longest_slice` and the API details are open for discussion. A draft implementation with documentation and tests is available at: https://github.com/ruby/ruby/pull/19838 -- https://bugs.ruby-lang.org/
Issue #22536 has been updated by datibbaw (Tjerk Meesters). ufuk (Ufuk Kayserilioglu) wrote in #note-3:
One concern that I have about this proposal is that the method proposed needs to iterate all the items to be sure that it found the longest slice, but an `Enumerable` can be infinite, which would guarantee that the method call would never return:
```ruby (1..).longest_slice(&:even) # does this ever return? ```
To put it in the words of Claude, you're absolutely right! This would obviously be a better fit for `Array`. In any case, I will admit that this solved a pretty specific problem for me that I only intuitively thought could be more generic :) ---------------------------------------- Feature #22536: Add Enumerable#longest_slice to return the range of the longest consecutive matching sequence https://bugs.ruby-lang.org/issues/22536#change-119968 * Author: datibbaw (Tjerk Meesters) * Status: Open ---------------------------------------- I propose adding `Enumerable#longest_slice`, which returns a Range identifying the longest consecutive sequence of matching entries. Without a block, entries match when they are truthy. Both `nil` and `false` break a sequence. With a block, entries match when the block returns a truthy value. ### Motivation Finding the longest consecutive matching sequence currently requires manually tracking positions and run lengths, or creating intermediate collections. Returning a Range makes the result directly usable for slicing an Array or identifying positions in another enumerable. I ran into this when analysing IPv6 compression logic and reasoned that it may have other uses; in comparison, longest prefix / suffix is already covered with e.g. `#index` and `#rindex` using block syntax. ### Examples [nil, 'a', 'b', nil, 'c'].longest_slice # => 1...3 [1, 2, 4, 3, 6].longest_slice(&:even?) # => 1...3 [false, 0, []].longest_slice # => 1...3 a = [10, 20, 30, 40] a.each.with_index(1).longest_slice do |value, index| value >= 20 && index <= 3 end # => 1...3 ### Proposed behavior * Return a zero-based, end-exclusive Range. * Return `0...0` for an empty enumerable or when no entries match. * When multiple matching sequences have the same maximum length, return the first. * Forward all arguments yielded by `each` to the block, supporting indexed enumerators and multi-argument iterators. * Always express the result in iteration positions, independently of any index yielded to the block. * Traverse the enumerable once, using constant auxiliary space. The name `longest_slice` and the API details are open for discussion. A draft implementation with documentation and tests is available at: https://github.com/ruby/ruby/pull/19838 -- https://bugs.ruby-lang.org/
Issue #22536 has been updated by datibbaw (Tjerk Meesters). I think #17056 actually solves the issue to enough of a degree that I may not need this anymore :) ---------------------------------------- Feature #22536: Add Enumerable#longest_slice to return the range of the longest consecutive matching sequence https://bugs.ruby-lang.org/issues/22536#change-120163 * Author: datibbaw (Tjerk Meesters) * Status: Open ---------------------------------------- I propose adding `Enumerable#longest_slice`, which returns a Range identifying the longest consecutive sequence of matching entries. Without a block, entries match when they are truthy. Both `nil` and `false` break a sequence. With a block, entries match when the block returns a truthy value. ### Motivation Finding the longest consecutive matching sequence currently requires manually tracking positions and run lengths, or creating intermediate collections. Returning a Range makes the result directly usable for slicing an Array or identifying positions in another enumerable. I ran into this when analysing IPv6 compression logic and reasoned that it may have other uses; in comparison, longest prefix / suffix is already covered with e.g. `#index` and `#rindex` using block syntax. ### Examples [nil, 'a', 'b', nil, 'c'].longest_slice # => 1...3 [1, 2, 4, 3, 6].longest_slice(&:even?) # => 1...3 [false, 0, []].longest_slice # => 1...3 a = [10, 20, 30, 40] a.each.with_index(1).longest_slice do |value, index| value >= 20 && index <= 3 end # => 1...3 ### Proposed behavior * Return a zero-based, end-exclusive Range. * Return `0...0` for an empty enumerable or when no entries match. * When multiple matching sequences have the same maximum length, return the first. * Forward all arguments yielded by `each` to the block, supporting indexed enumerators and multi-argument iterators. * Always express the result in iteration positions, independently of any index yielded to the block. * Traverse the enumerable once, using constant auxiliary space. The name `longest_slice` and the API details are open for discussion. A draft implementation with documentation and tests is available at: https://github.com/ruby/ruby/pull/19838 -- https://bugs.ruby-lang.org/
participants (3)
-
datibbaw (Tjerk Meesters) -
duerst -
ufuk (Ufuk Kayserilioglu)