Skip to content

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

Merged
merged 6 commits into from
Mar 23, 2021
Merged

Add Sequence.firstNonNil(_:) #31

merged 6 commits into from
Mar 23, 2021

Conversation

Qata
Copy link
Contributor

@Qata Qata commented Oct 21, 2020

Resolves #21

Description

firstNonNil(_:) returns the first value that is resolved by the passed transform to a non nil (.some) value.

Detailed Design

extension Sequence {
    /// Returns the first element in `self` that `transform` maps to a `.some`.
    func firstNonNil<Result>(_ transform: (Element) throws -> Result?) rethrows -> Result?
}

Documentation Plan

Added documentation comments to the new function.

Test Plan

Five tests have been added.

Source Impact

None

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

Copy link
Member

@ktoso ktoso left a 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?

@Qata
Copy link
Contributor Author

Qata commented Nov 3, 2020

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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

@xwu xwu left a 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
Copy link
Contributor

@xwu xwu Nov 4, 2020

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.
Copy link
Contributor

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.
Copy link
Contributor

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.

Copy link
Member

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?

@Qata Qata changed the title Add Sequence.firstNonNil(of:) Add Sequence.firstNonNil(_:) Nov 4, 2020

### Naming

This method’s name was selected for its comprehensibility.
Copy link
Member

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`.
Copy link
Member

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?

Comment on lines +21 to +27
for value in self {
if let value = try transform(value) {
return value
}
}
return nil
}
Copy link
Member

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.

@timvermeulen
Copy link
Contributor

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.

@timvermeulen
Copy link
Contributor

@swift-ci please test

@timvermeulen timvermeulen merged commit e64cff7 into apple:main Mar 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A first operator that returns the first non-nil transformed value
5 participants