Skip to content

Commit

Permalink
Use default select/reject implementation for SortedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
GarrisonJ committed May 16, 2024
1 parent 01f18d4 commit c32460f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 34 deletions.
31 changes: 0 additions & 31 deletions lib/sorted_containers/sorted_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -620,25 +620,6 @@ def fill(*args, &block)
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/MethodLength

# Calls the block, if given, with each element of +self+;
# returns a new SortedArray containing those elements of +self+
# for which the block returns a truthy value.
#
# If no block is given, an Enumerator is returned instead.
#
# @yield [value] The block to filter with.
# @return [SortedArray, Enumerator] The filtered array.
def select
return to_enum(:select) unless block_given?

new_values = []
each do |value|
new_values << value if yield(value)
end
self.class.new(new_values, load_factor: @load_factor)
end
alias filter select

# Calls the block, if given, with each element of +self+;
# returns +self+ with the elements for which the block returns a truthy value.
#
Expand Down Expand Up @@ -1034,18 +1015,6 @@ def rassoc(obj)
index.nil? ? nil : self[index]
end

# Returns a new SortedArray whose elements are all those from +self+ for which the block returns false or nil.
#
# Returns an Enumerator if no block is given.
#
# @yield [value] The block to reject with.
# @return [SortedArray, Enumerator] The rejected array.
def reject
return to_enum(:reject) unless block_given?

select { |value| !yield(value) }
end

# Deletes every element of +self+ for which block evaluates to true.
#
# Returns +self+ if any changes were made, otherwise returns +nil+.
Expand Down
6 changes: 3 additions & 3 deletions spec/sorted_array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ def dig(key)
describe "select" do
it "should return the values that meet the given criterion" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array.select { |i| i > 3 }).to eq(SortedContainers::SortedArray.new([4, 5]))
expect(array.select { |i| i > 3 }).to eq([4, 5])
end

it "should return an enumerator if no block is given" do
Expand All @@ -1040,7 +1040,7 @@ def dig(key)

it "filter should be an alias for select" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array.filter { |i| i > 3 }).to eq(SortedContainers::SortedArray.new([4, 5]))
expect(array.filter { |i| i > 3 }).to eq([4, 5])
end
end

Expand Down Expand Up @@ -1697,7 +1697,7 @@ def dig(key)
describe "reject" do
it "should reject elements that meet the given criterion" do
array = SortedContainers::SortedArray.new([1, 2, 3, 4, 5])
expect(array.reject { |i| i > 3 }).to eq(SortedContainers::SortedArray.new([1, 2, 3]))
expect(array.reject { |i| i > 3 }).to eq([1, 2, 3])
end

it "should not modify the original array" do
Expand Down

0 comments on commit c32460f

Please sign in to comment.