Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add find and find! methods to Indexable #15552

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions spec/std/indexable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,50 @@ describe Indexable do
end
end

describe "#find" do
it "finds the element matching the block" do
indexable = SafeIndexable.new(4)
indexable.find { |i| i > 2 }.should eq 3
end

it "finds the element matching the block after given offset" do
indexable = SafeIndexable.new(8)
indexable.find(5) { |i| i.even? }.should eq 6
end

it "does not find the element matching the block" do
indexable = SafeIndexable.new(4)
indexable.find { |i| i > 7 }.should be_nil
end

it "does not find the element matching the block, returns custom if_none value" do
indexable = SafeIndexable.new(4)
indexable.find(if_none: -1) { |i| i > 7 }.should eq -1
end

it "does not find the element matching the block after given offset, returns custom if_none value" do
indexable = SafeIndexable.new(5)
indexable.find(3, -3) { |i| i > 15 }.should eq -3
end
end

describe "#find!" do
it "finds the element matching the block" do
indexable = SafeIndexable.new(4)
indexable.find! { |i| i > 2 }.should eq 3
end

it "finds the element matching the block after given offset" do
indexable = SafeIndexable.new(8)
indexable.find!(5) { |i| i.even? }.should eq 6
end

it "does not find the element matching the block, raises not found" do
indexable = SafeIndexable.new(4)
expect_raises(Enumerable::NotFoundError) { indexable.find! { |i| i > 7 } }
end
end

describe "#rindex" do
it "does rindex with big negative offset" do
indexable = SafeIndexable.new(3)
Expand Down
29 changes: 29 additions & 0 deletions src/indexable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,35 @@ module Indexable(T)
index(offset) { |e| yield e } || raise Enumerable::NotFoundError.new
end

# Returns the first element in the indexable for which the passed block
# is truthy, starting from the given *offset*.
#
# Accepts an optional parameter *if_none*, to set what gets returned if
# no element is found (defaults to `nil`).
#
# ```
# [1, 2, 3, 4].find { |i| i > 2 } # => 3
# [1, 2, 3, 4].find(-1, 2) { |i| i < 2 } # => -1
# [1, 2, 3, 4].find(-1) { |i| i > 8 } # => -1
# ```
def find(offset : Int = 0, if_none = nil, & : T ->)
offset += size if offset < 0
return nil if offset < 0
return (index(offset) { |i| yield i }).try { |i| unsafe_fetch(i) } || if_none
end

# Returns the first element in the indexable for which the passed block is truthy.
# Raises `Enumerable::NotFoundError` if there is no element for which the block is truthy.
#
# ```
# [1, 2, 3, 4].find! { |i| i > 2 } # => 3
# [1, 2, 3, 4].find! { |i| i > 8 } # => raises Enumerable::NotFoundError
def find!(offset : Int = 0, & : T ->)
offset += size if offset < 0
return nil if offset < 0
return (index(offset) { |i| yield i }).try { |i| unsafe_fetch(i) } || raise Enumerable::NotFoundError.new
end

# Returns the last element of `self` if it's not empty, or raises `IndexError`.
#
# ```
Expand Down