Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?
Add Iterators.first and Iterators.last which don't throw for empty collections #37119
Changes from 2 commits
39a39e9
c89a724
f477520
8c425f7
16a19f9
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
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.
I'm not thrilled about
Some
here — I don't think we have a single API in base that returnsSome
. That said, this is unlike the other find/search APIs in that they return indices orRegexMatch
es or values from some other limited type set that does not includeNothing
.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.
I think I've convinced myself that at the very least this should be a separate function from
first
/last
. DoesIterators.first
(as suggested in OP) sound like a good name to you? In that case, we wouldn't have to create a predicate accepting version as it would compose withIterators.filter
. EDIT: this PR now implementsIterators.first
instead.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
Union{Some{T},Nothing}
. This includesfirst
,last
,getindex
,reduce
,foldl
, etc. This is obviously breaking; I should probably create a "Taking something seriously" issue to get feedback on that idea for julia 2.0.