mirror of
https://github.com/apple/swift-algorithms
synced 2026-11-29 04:44:21 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f0df883f88 |
No files matched your search
+1
-37
@@ -16,33 +16,6 @@ package updates, you can specify your package dependency using
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## [0.0.4] - 2021-03-29
|
|
||||||
|
|
||||||
### Additions
|
|
||||||
|
|
||||||
More new algorithms to join the party:
|
|
||||||
|
|
||||||
- A lazy version of the standard library's two `split` methods. ([#78])
|
|
||||||
- `firstNonNil(_:)` returns the first non-`nil` element from an
|
|
||||||
optional-generating transform. ([#31])
|
|
||||||
- `uniquePermutations()` skips duplicates when generating permutations of a
|
|
||||||
collection. ([#91])
|
|
||||||
- The `reductions` methods return all the in-between states of reducing a
|
|
||||||
sequence or collection. ([#46])
|
|
||||||
|
|
||||||
### Fixes
|
|
||||||
|
|
||||||
- Methods and computed properties are more consistently marked as inlinable,
|
|
||||||
resolving a performance regression.
|
|
||||||
- The `Stride` type now efficiently calculates distances between positions,
|
|
||||||
supported by the underlying collection.
|
|
||||||
- Better test coverage and improved diagnostics for comparing sequences.
|
|
||||||
- Fixed links and improved documentation.
|
|
||||||
|
|
||||||
The 0.0.4 release includes contributions from [bjhomer], [danielctull],
|
|
||||||
[hashemi], [karwa], [kylemacomber], [LucianoPAlmeida], [mdznr], [natecook1000],
|
|
||||||
[ollieatkinson], [Qata], [timvermeulen], and [toddthomas]. Thank you!
|
|
||||||
|
|
||||||
## [0.0.3] - 2021-02-26
|
## [0.0.3] - 2021-02-26
|
||||||
|
|
||||||
### Additions
|
### Additions
|
||||||
@@ -123,8 +96,7 @@ This changelog's format is based on [Keep a Changelog](https://keepachangelog.co
|
|||||||
|
|
||||||
<!-- Link references for releases -->
|
<!-- Link references for releases -->
|
||||||
|
|
||||||
[Unreleased]: https://github.com/apple/swift-algorithms/compare/0.0.4...HEAD
|
[Unreleased]: https://github.com/apple/swift-algorithms/compare/0.0.3...HEAD
|
||||||
[0.0.4]: https://github.com/apple/swift-algorithms/compare/0.0.3...0.0.4
|
|
||||||
[0.0.3]: https://github.com/apple/swift-algorithms/compare/0.0.2...0.0.3
|
[0.0.3]: https://github.com/apple/swift-algorithms/compare/0.0.2...0.0.3
|
||||||
[0.0.2]: https://github.com/apple/swift-algorithms/compare/0.0.1...0.0.2
|
[0.0.2]: https://github.com/apple/swift-algorithms/compare/0.0.1...0.0.2
|
||||||
[0.0.1]: https://github.com/apple/swift-algorithms/releases/tag/0.0.1
|
[0.0.1]: https://github.com/apple/swift-algorithms/releases/tag/0.0.1
|
||||||
@@ -135,26 +107,20 @@ This changelog's format is based on [Keep a Changelog](https://keepachangelog.co
|
|||||||
[#9]: https://github.com/apple/swift-algorithms/pull/9
|
[#9]: https://github.com/apple/swift-algorithms/pull/9
|
||||||
[#20]: https://github.com/apple/swift-algorithms/pull/20
|
[#20]: https://github.com/apple/swift-algorithms/pull/20
|
||||||
[#24]: https://github.com/apple/swift-algorithms/pull/24
|
[#24]: https://github.com/apple/swift-algorithms/pull/24
|
||||||
[#31]: https://github.com/apple/swift-algorithms/pull/31
|
|
||||||
[#35]: https://github.com/apple/swift-algorithms/pull/35
|
[#35]: https://github.com/apple/swift-algorithms/pull/35
|
||||||
[#46]: https://github.com/apple/swift-algorithms/pull/46
|
|
||||||
[#51]: https://github.com/apple/swift-algorithms/pull/51
|
[#51]: https://github.com/apple/swift-algorithms/pull/51
|
||||||
[#54]: https://github.com/apple/swift-algorithms/pull/54
|
[#54]: https://github.com/apple/swift-algorithms/pull/54
|
||||||
[#56]: https://github.com/apple/swift-algorithms/pull/56
|
[#56]: https://github.com/apple/swift-algorithms/pull/56
|
||||||
[#65]: https://github.com/apple/swift-algorithms/pull/65
|
[#65]: https://github.com/apple/swift-algorithms/pull/65
|
||||||
[#77]: https://github.com/apple/swift-algorithms/pull/77
|
[#77]: https://github.com/apple/swift-algorithms/pull/77
|
||||||
[#78]: https://github.com/apple/swift-algorithms/pull/78
|
|
||||||
[#91]: https://github.com/apple/swift-algorithms/pull/91
|
|
||||||
|
|
||||||
<!-- Link references for contributors -->
|
<!-- Link references for contributors -->
|
||||||
|
|
||||||
[AustinConlon]: https://github.com/apple/swift-algorithms/commits?author=AustinConlon
|
[AustinConlon]: https://github.com/apple/swift-algorithms/commits?author=AustinConlon
|
||||||
[benrimmington]: https://github.com/apple/swift-algorithms/commits?author=benrimmington
|
[benrimmington]: https://github.com/apple/swift-algorithms/commits?author=benrimmington
|
||||||
[bjhomer]: https://github.com/apple/swift-algorithms/commits?author=bjhomer
|
|
||||||
[danielctull]: https://github.com/apple/swift-algorithms/commits?author=danielctull
|
[danielctull]: https://github.com/apple/swift-algorithms/commits?author=danielctull
|
||||||
[dhruvshah8]: https://github.com/apple/swift-algorithms/commits?author=dhruvshah8
|
[dhruvshah8]: https://github.com/apple/swift-algorithms/commits?author=dhruvshah8
|
||||||
[egorzhdan]: https://github.com/apple/swift-algorithms/commits?author=egorzhdan
|
[egorzhdan]: https://github.com/apple/swift-algorithms/commits?author=egorzhdan
|
||||||
[hashemi]: https://github.com/apple/swift-algorithms/commits?author=hashemi
|
|
||||||
[IanKeen]: https://github.com/apple/swift-algorithms/commits?author=IanKeen
|
[IanKeen]: https://github.com/apple/swift-algorithms/commits?author=IanKeen
|
||||||
[iSame7]: https://github.com/apple/swift-algorithms/commits?author=iSame7
|
[iSame7]: https://github.com/apple/swift-algorithms/commits?author=iSame7
|
||||||
[karwa]: https://github.com/apple/swift-algorithms/commits?author=karwa
|
[karwa]: https://github.com/apple/swift-algorithms/commits?author=karwa
|
||||||
@@ -167,7 +133,6 @@ This changelog's format is based on [Keep a Changelog](https://keepachangelog.co
|
|||||||
[nordicio]: https://github.com/apple/swift-algorithms/commits?author=nordicio
|
[nordicio]: https://github.com/apple/swift-algorithms/commits?author=nordicio
|
||||||
[ollieatkinson]: https://github.com/apple/swift-algorithms/commits?author=ollieatkinson
|
[ollieatkinson]: https://github.com/apple/swift-algorithms/commits?author=ollieatkinson
|
||||||
[pmtao]: https://github.com/apple/swift-algorithms/commits?author=pmtao
|
[pmtao]: https://github.com/apple/swift-algorithms/commits?author=pmtao
|
||||||
[Qata]: https://github.com/apple/swift-algorithms/commits?author=Qata
|
|
||||||
[rakaramos]: https://github.com/apple/swift-algorithms/commits?author=rakaramos
|
[rakaramos]: https://github.com/apple/swift-algorithms/commits?author=rakaramos
|
||||||
[rockbruno]: https://github.com/apple/swift-algorithms/commits?author=rockbruno
|
[rockbruno]: https://github.com/apple/swift-algorithms/commits?author=rockbruno
|
||||||
[Roshankumar350]: https://github.com/apple/swift-algorithms/commits?author=Roshankumar350
|
[Roshankumar350]: https://github.com/apple/swift-algorithms/commits?author=Roshankumar350
|
||||||
@@ -175,4 +140,3 @@ This changelog's format is based on [Keep a Changelog](https://keepachangelog.co
|
|||||||
[sidepelican]: https://github.com/apple/swift-algorithms/commits?author=sidepelican
|
[sidepelican]: https://github.com/apple/swift-algorithms/commits?author=sidepelican
|
||||||
[stephentyrone]: https://github.com/apple/swift-algorithms/commits?author=stephentyrone
|
[stephentyrone]: https://github.com/apple/swift-algorithms/commits?author=stephentyrone
|
||||||
[timvermeulen]: https://github.com/apple/swift-algorithms/commits?author=timvermeulen
|
[timvermeulen]: https://github.com/apple/swift-algorithms/commits?author=timvermeulen
|
||||||
[toddthomas]: https://github.com/apple/swift-algorithms/commits?author=toddthomas
|
|
||||||
+18
-60
@@ -2,48 +2,23 @@
|
|||||||
|
|
||||||
[[Source](https://github.com/apple/swift-algorithms/blob/main/Sources/Algorithms/FirstNonNil.swift) |
|
[[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)]
|
[Tests](https://github.com/apple/swift-algorithms/blob/main/Tests/SwiftAlgorithmsTests/FirstNonNilTests.swift)]
|
||||||
|
|
||||||
Returns the first non-`nil` result obtained from applying the given
|
Retrieves the first `.some` encountered while applying the given transform.
|
||||||
transformation to the elements of the sequence.
|
|
||||||
|
This operation is available through the `firstNonNil(_:)` method on any sequence.
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
let strings = ["three", "3.14", "-5", "2"]
|
let value = ["A", "B", "10"].firstNonNil { Int($0) }
|
||||||
if let firstInt = strings.firstNonNil({ Int($0) }) {
|
// value == .some(10)
|
||||||
print(firstInt)
|
//
|
||||||
// -5
|
let noValue = ["A", "B", "C"].firstNonNil { Int($0) }
|
||||||
}
|
// noValue == .none
|
||||||
```
|
```
|
||||||
|
|
||||||
Like `first(where:)`, this method stops iterating the sequence once a match is
|
|
||||||
found. Use `firstNonNil(_:)` to avoid having to apply a transformation twice:
|
|
||||||
|
|
||||||
```swift
|
This method is analogous to `first(where:)` in how it only consumes values until
|
||||||
let strings = ["three", "3.14", "-5", "2"]
|
a `.some` is found, unlike using lazy operators, which will load any sequence into a collection
|
||||||
if let firstIntAsString = strings.first(where: { Int($0) != nil }) {
|
before evaluating its transforms lazily.
|
||||||
let firstInt = Int(firstIntAsString)! // :(
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
This method's behavior can also be approximated using `compactMap(_:)`:
|
|
||||||
|
|
||||||
```swift
|
|
||||||
let strings = ["three", "3.14", "-5", "2"]
|
|
||||||
if let firstInt = strings.compactMap({ Int($0) }).first {
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
However, unlike `firstNonNil(_:)` and `first(where:)`, `compactMap(_:)` does not
|
|
||||||
stop iterating the sequence once the first match is found. Adding `.lazy` fixes
|
|
||||||
this, at the cost of requiring an escaping closure that you cannot `throw` from:
|
|
||||||
|
|
||||||
```swift
|
|
||||||
let strings = ["three", "3.14", "-5", "2"]
|
|
||||||
if let firstInt = strings.lazy.compactMap({ Int($0) }).first {
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Detailed Design
|
## Detailed Design
|
||||||
|
|
||||||
@@ -52,34 +27,17 @@ protocol:
|
|||||||
|
|
||||||
```swift
|
```swift
|
||||||
public extension Sequence {
|
public extension Sequence {
|
||||||
func firstNonNil<Result>(
|
func firstNonNil<Result>(_ transform: (Element) throws -> Result?)
|
||||||
_ transform: (Element) throws -> Result?
|
rethrows -> Result?
|
||||||
) rethrows -> Result?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Complexity
|
|
||||||
|
|
||||||
`firstNonNil(_:)` is an O(*n*) operation, where *n* is the number of
|
|
||||||
elements at the start of the sequence that result in `nil` when applying the
|
|
||||||
transformation.
|
|
||||||
|
|
||||||
### Naming
|
### Naming
|
||||||
|
|
||||||
Some alternative names were considered:
|
This method’s name was selected for its comprehensibility.
|
||||||
|
|
||||||
* `compactMapFirst(_:)`
|
|
||||||
* `first(mapping:)`
|
|
||||||
* `firstSome(_:)`
|
|
||||||
* `firstMap(_:)`
|
|
||||||
|
|
||||||
### Comparison with other languages
|
### Comparison with other languages
|
||||||
|
|
||||||
**Haskell**: Haskell includes the `firstJust` method, which has the same
|
**Scala**: Scala provides a `collectFirst` function that finds the first element
|
||||||
semantics as the `firstNonNil(_:)` method.
|
in a collection for which a partial function is defined.
|
||||||
|
|
||||||
**Rust**: Rust includes the `find_map` method, which has the same semantics as
|
|
||||||
the `firstNonNil(_:)` method.
|
|
||||||
|
|
||||||
**Scala**: Scala includes the `collectFirst` method, which has the same
|
|
||||||
semantics as the `firstNonNil(_:)` method.
|
|
||||||
@@ -3,7 +3,9 @@
|
|||||||
[[Source](https://github.com/apple/swift-algorithms/blob/main/Sources/Algorithms/Reductions.swift) |
|
[[Source](https://github.com/apple/swift-algorithms/blob/main/Sources/Algorithms/Reductions.swift) |
|
||||||
[Tests](https://github.com/apple/swift-algorithms/blob/main/Tests/SwiftAlgorithmsTests/ReductionsTests.swift)]
|
[Tests](https://github.com/apple/swift-algorithms/blob/main/Tests/SwiftAlgorithmsTests/ReductionsTests.swift)]
|
||||||
|
|
||||||
Produces all the intermediate results of reducing a sequence's elements using a closure.
|
Produces a sequence of values.
|
||||||
|
|
||||||
|
This has the behaviour of reduce, but also returns all intermediate results.
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
let exclusiveRunningTotal = (1...5).reductions(0, +)
|
let exclusiveRunningTotal = (1...5).reductions(0, +)
|
||||||
|
|||||||
@@ -27,9 +27,7 @@ Read more about the package, and the intent behind it, in the [announcement on s
|
|||||||
|
|
||||||
- [`randomSample(count:)`, `randomSample(count:using:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/RandomSampling.md): Randomly selects a specific number of elements from a collection.
|
- [`randomSample(count:)`, `randomSample(count:using:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/RandomSampling.md): Randomly selects a specific number of elements from a collection.
|
||||||
- [`randomStableSample(count:)`, `randomStableSample(count:using:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/RandomSampling.md): Randomly selects a specific number of elements from a collection, preserving their original relative order.
|
- [`randomStableSample(count:)`, `randomStableSample(count:using:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/RandomSampling.md): Randomly selects a specific number of elements from a collection, preserving their original relative order.
|
||||||
- [`striding(by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Stride.md): Returns every nth element of a collection.
|
|
||||||
- [`suffix(while:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Suffix.md): Returns the suffix of a collection where all element pass a given predicate.
|
- [`suffix(while:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Suffix.md): Returns the suffix of a collection where all element pass a given predicate.
|
||||||
- [`trimming(while:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Trim.md): Returns a slice by trimming elements from a collection's start and end.
|
|
||||||
- [`uniqued()`, `uniqued(on:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Unique.md): The unique elements of a collection, preserving their order.
|
- [`uniqued()`, `uniqued(on:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Unique.md): The unique elements of a collection, preserving their order.
|
||||||
|
|
||||||
#### Partial sorting
|
#### Partial sorting
|
||||||
@@ -39,12 +37,12 @@ Read more about the package, and the intent behind it, in the [announcement on s
|
|||||||
#### Other useful operations
|
#### Other useful operations
|
||||||
|
|
||||||
- [`chunked(by:)`, `chunked(on:)`, `chunks(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Chunked.md): Eager and lazy operations that break a collection into chunks based on either a binary predicate or when the result of a projection changes or chunks of a given count.
|
- [`chunked(by:)`, `chunked(on:)`, `chunks(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Chunked.md): Eager and lazy operations that break a collection into chunks based on either a binary predicate or when the result of a projection changes or chunks of a given count.
|
||||||
- [`firstNonNil(_:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/FirstNonNil.md): Returns the first non-`nil` result from transforming a sequence's elements.
|
|
||||||
- [`indexed()`](https://github.com/apple/swift-algorithms/blob/main/Guides/Indexed.md): Iterate over tuples of a collection's indices and elements.
|
- [`indexed()`](https://github.com/apple/swift-algorithms/blob/main/Guides/Indexed.md): Iterate over tuples of a collection's indices and elements.
|
||||||
- [`interspersed(with:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Intersperse.md): Place a value between every two elements of a sequence.
|
- [`interspersed(with:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Intersperse.md): Place a value between every two elements of a sequence.
|
||||||
- [`reductions(_:)`, `reductions(_:_:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Reductions.md): Returns all the intermediate states of reducing the elements of a sequence or collection.
|
- [`trimming(while:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Trim.md): Returns a slice by trimming elements from a collection's start and end.
|
||||||
- [`split(maxSplits:omittingEmptySubsequences:whereSeparator)`, `split(separator:maxSplits:omittingEmptySubsequences)`](https://github.com/apple/swift-algorithms/blob/main/Guides/LazySplit.md): Lazy versions of the Standard Library's eager operations that split sequences and collections into subsequences separated by the specified separator element.
|
|
||||||
- [`windows(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Windows.md): Breaks a collection into overlapping subsequences where elements are slices from the original collection.
|
- [`windows(ofCount:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Windows.md): Breaks a collection into overlapping subsequences where elements are slices from the original collection.
|
||||||
|
- [`striding(by:)`](https://github.com/apple/swift-algorithms/blob/main/Guides/Stride.md): Returns every nth element of a collection.
|
||||||
|
- [`split(maxSplits:omittingEmptySubsequences:whereSeparator)`, `split(separator:maxSplits:omittingEmptySubsequences)`](https://github.com/apple/swift-algorithms/blob/main/Guides/LazySplit.md): Lazy versions of the Standard Library's eager operations that split sequences and collections into subsequences separated by the specified separator element.
|
||||||
|
|
||||||
## Adding Swift Algorithms as a Dependency
|
## Adding Swift Algorithms as a Dependency
|
||||||
|
|
||||||
|
|||||||
@@ -14,30 +14,15 @@
|
|||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
public extension Sequence {
|
public extension Sequence {
|
||||||
/// Returns the first non-`nil` result obtained from applying the given
|
/// Returns the first element in `self` that `transform` maps to a `.some`.
|
||||||
/// transformation to the elements of the sequence.
|
|
||||||
///
|
|
||||||
/// let strings = ["three", "3.14", "-5", "2"]
|
|
||||||
/// if let firstInt = strings.firstNonNil({ Int($0) }) {
|
|
||||||
/// print(firstInt)
|
|
||||||
/// // -5
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// - Parameter transform: A closure that takes an element of the sequence as
|
|
||||||
/// its argument and returns an optional transformed value.
|
|
||||||
/// - Returns: The first non-`nil` return value of the transformation, or
|
|
||||||
/// `nil` if no transformation is successful.
|
|
||||||
///
|
|
||||||
/// - Complexity: O(*n*), where *n* is the number of elements at the start of
|
|
||||||
/// the sequence that result in `nil` when applying the transformation.
|
|
||||||
func firstNonNil<Result>(
|
func firstNonNil<Result>(
|
||||||
_ transform: (Element) throws -> Result?
|
_ transform: (Element) throws -> Result?
|
||||||
) rethrows -> Result? {
|
) rethrows -> Result? {
|
||||||
for value in self {
|
for value in self {
|
||||||
if let value = try transform(value) {
|
if let value = try transform(value) {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user