-
Notifications
You must be signed in to change notification settings - Fork 446
Add Sequence.firstNonNil(_:) #31
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
Conversation
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.
Looks okey :) Should probably also gain a Guides/
doc?
Minor: Maybe consider keeping the ticket open and adding the usual Resolves #21 here rather than closing ticket immediately?
This feature felt minimal and self-explanatory enough to not be worth a guides doc, but I'll add one! |
Guides/First.md
Outdated
@@ -0,0 +1,45 @@ | |||
# First |
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.
Since this API isn't first
, but firstNonNil
, can we align the naming of documents and headings accordingly?
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.
My thinking that it was a general category of functions for which others might be added. I'll amend it.
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.
It looks like we’ve settled on a name of firstNonNil(_:)
. Can you update the pull request title and message? They currently say that we’re adding an API called firstNonNil(of:)
.
|
||
|
||
This method is analogous to `first(where:)` in how it only consumes values until | ||
a `.some` is found, unlike using lazy operators, which will load any sequence into a collection |
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.
Could you be a little more explicit here about why this method is useful? I think @timvermeulen explained it very well in the corresponding issue (about using the alternative being unintentionally not lazy in certain situations), and if you get his permission, that might be a helpful piece of information for users.
### Comparison with other languages | ||
|
||
**Scala**: Scala provides a `collectFirst` function that finds the first element | ||
in a collection for which a partial function is defined. |
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.
There were a few other precedents mentioned in the corresponding issue. Could you please include them here? Ideally, we’d look around a bit more and really study the landscape here so that we can be sure we’ve picked a good name.
|
||
### Naming | ||
|
||
This method’s name was selected for its comprehensibility. |
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 don’t think this sentence adds much to the reader. If this section isn’t mandatory, then it can be deleted. If it is mandatory, then you’ll need to explain why this name has good “comprehensibility.” Perhaps @natecook1000 can help you to explain since he made the choice.
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.
It would be great to capture the other names that came up in the issue discussion here. Why was firstNonNil
chosen over those?
Co-authored-by: Xiaodi Wu <[email protected]>
Co-authored-by: Xiaodi Wu <[email protected]>
|
||
### Naming | ||
|
||
This method’s name was selected for its comprehensibility. |
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.
It would be great to capture the other names that came up in the issue discussion here. Why was firstNonNil
chosen over those?
//===----------------------------------------------------------------------===// | ||
|
||
public extension Sequence { | ||
/// Returns the first element in `self` that `transform` maps to a `.some`. |
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 method doesn't return the first element, it returns the mapped result. Can you update this wording and include an example demonstrating the usage?
for value in self { | ||
if let value = try transform(value) { | ||
return value | ||
} | ||
} | ||
return nil | ||
} |
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.
Nit: For source files we use 2-space indents.
Hey @Qata, this is to let you know we'll soon be landing this addition and making the minor changes necessary in order to ship this in the next release. 🚀 Thank you for your work on this, and feel free to make any changes you still want to make in the meantime. |
@swift-ci please test |
Resolves #21
Description
firstNonNil(_:)
returns the first value that is resolved by the passed transform to a nonnil
(.some
) value.Detailed Design
Documentation Plan
Added documentation comments to the new function.
Test Plan
Five tests have been added.
Source Impact
None
Checklist