mirror of
https://github.com/apple/swift-algorithms
synced 2026-11-27 04:34:21 +00:00
* Add Sequence.firstNonNil(of:) and associated tests * Rename First.swift to FirstTests.swift * Address PR comments by adding guide and changing naming * Address PR comments by changing name * Update Guides/FirstNonNil.md Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com> * Update Sources/Algorithms/FirstNonNil.swift Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com> Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>
44 lines
1.3 KiB
Markdown
44 lines
1.3 KiB
Markdown
# First Non-Nil
|
||
|
||
[[Source](https://github.com/apple/swift-algorithms/blob/main/Sources/Algorithms/FirstNonNil.swift) |
|
||
[Tests](https://github.com/apple/swift-algorithms/blob/main/Tests/SwiftAlgorithmsTests/FirstNonNilTests.swift)]
|
||
|
||
Retrieves the first `.some` encountered while applying the given transform.
|
||
|
||
This operation is available through the `firstNonNil(_:)` method on any sequence.
|
||
|
||
```swift
|
||
let value = ["A", "B", "10"].firstNonNil { Int($0) }
|
||
// value == .some(10)
|
||
//
|
||
let noValue = ["A", "B", "C"].firstNonNil { Int($0) }
|
||
// noValue == .none
|
||
```
|
||
|
||
|
||
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
|
||
before evaluating its transforms lazily.
|
||
|
||
## Detailed Design
|
||
|
||
The `firstNonNil(_:)` method is added as an extension method on the `Sequence`
|
||
protocol:
|
||
|
||
```swift
|
||
public extension Sequence {
|
||
func firstNonNil<Result>(_ transform: (Element) throws -> Result?)
|
||
rethrows -> Result?
|
||
}
|
||
|
||
```
|
||
|
||
### Naming
|
||
|
||
This method’s name was selected for its comprehensibility.
|
||
|
||
### 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.
|