-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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 Iterators.first and Iterators.last which don't throw for empty collections #37119
base: master
Are you sure you want to change the base?
Changes from 4 commits
39a39e9
c89a724
f477520
8c425f7
16a19f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -343,6 +343,38 @@ function first(itr) | |
x[1] | ||
end | ||
|
||
""" | ||
first(predicate, coll) | ||
|
||
Get the first element of `coll` satisfying `predicate` wrapped in [`Some`](@ref). | ||
|
||
If no element of `coll` satisfies `predicate`, return `nothing`. | ||
|
||
non-Jedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
!!! compat "Julia 1.6" | ||
This method was added in Julia 1.6. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> first(>(5), 1:10) | ||
Some(6) | ||
|
||
julia> isnothing(first(isodd, 2:2:10)) | ||
true | ||
|
||
julia> something(first(iseven, [5, 3, 4, 2, 6, 8])) | ||
4 | ||
|
||
julia> something(first(>(10), 1:10), 0) | ||
0 | ||
``` | ||
""" | ||
function first(predicate, itr) | ||
for x in itr | ||
predicate(x) && return Some(x) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not thrilled about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I've convinced myself that at the very least this should be a separate function from In reality, I think all APIs that generically return an element of a collection but throw if the element isn't present should instead return |
||
end | ||
non-Jedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nothing | ||
end | ||
|
||
""" | ||
first(itr, n::Integer) | ||
|
||
|
@@ -388,6 +420,47 @@ julia> last([1; 2; 3; 4]) | |
""" | ||
last(a) = a[end] | ||
|
||
""" | ||
last(predicate, coll) | ||
|
||
Get the last element of `coll` satisfying `predicate` wrapped in [`Some`](@ref). | ||
|
||
If no element of `coll` satisfies `predicate`, return `nothing`. | ||
|
||
non-Jedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
!!! compat "Julia 1.6" | ||
This method was added in Julia 1.6. | ||
|
||
# Examples | ||
```jldoctest | ||
julia> last(<(5), 1:10) | ||
Some(4) | ||
|
||
julia> isnothing(last(isodd, 2:2:10)) | ||
true | ||
|
||
julia> something(last(iseven, [5; 3; 4; 2; 6; 9])) | ||
6 | ||
|
||
julia> something(last(>(10), 1:10), 0) | ||
0 | ||
``` | ||
""" | ||
function last(predicate, itr) | ||
out = nothing | ||
for x in itr | ||
non-Jedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
out = ifelse(predicate(x), Some(x), out) | ||
end | ||
return out | ||
end | ||
|
||
# faster version for arrays | ||
function last(predicate, a::AbstractArray) | ||
@inbounds for i in reverse(eachindex(a)) | ||
predicate(a[i]) && return Some(a[i]) | ||
end | ||
non-Jedi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nothing | ||
end | ||
|
||
""" | ||
last(itr, n::Integer) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is interesting — and deviates from the existing
first
which would throw an error. This gives me a bit of pause, but at a minimum it means that we cannot combine these docstrings:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point on the docstrings.