Implement operator specializations for Publishers.Sequence
This commit is contained in:
committed by
Sergej Jaskiewicz
parent
979c787331
commit
5340f33961
@@ -3659,124 +3659,6 @@ extension Publishers.Last : Equatable where Upstream : Equatable {
|
||||
public static func == (lhs: Publishers.Last<Upstream>, rhs: Publishers.Last<Upstream>) -> Bool
|
||||
}
|
||||
|
||||
extension Publishers.Sequence {
|
||||
|
||||
public func allSatisfy(_ predicate: (Publishers.Sequence<Elements, Failure>.Output) -> Bool) -> Publishers.Once<Bool, Failure>
|
||||
|
||||
public func tryAllSatisfy(_ predicate: (Publishers.Sequence<Elements, Failure>.Output) throws -> Bool) -> Publishers.Once<Bool, Error>
|
||||
|
||||
public func collect() -> Publishers.Once<[Publishers.Sequence<Elements, Failure>.Output], Failure>
|
||||
|
||||
public func compactMap<T>(_ transform: (Publishers.Sequence<Elements, Failure>.Output) -> T?) -> Publishers.Sequence<[T], Failure>
|
||||
|
||||
public func min(by areInIncreasingOrder: (Publishers.Sequence<Elements, Failure>.Output, Publishers.Sequence<Elements, Failure>.Output) -> Bool) -> Publishers.Optional<Publishers.Sequence<Elements, Failure>.Output, Failure>
|
||||
|
||||
public func tryMin(by areInIncreasingOrder: (Publishers.Sequence<Elements, Failure>.Output, Publishers.Sequence<Elements, Failure>.Output) throws -> Bool) -> Publishers.Optional<Publishers.Sequence<Elements, Failure>.Output, Error>
|
||||
|
||||
public func max(by areInIncreasingOrder: (Publishers.Sequence<Elements, Failure>.Output, Publishers.Sequence<Elements, Failure>.Output) -> Bool) -> Publishers.Optional<Publishers.Sequence<Elements, Failure>.Output, Failure>
|
||||
|
||||
public func tryMax(by areInIncreasingOrder: (Publishers.Sequence<Elements, Failure>.Output, Publishers.Sequence<Elements, Failure>.Output) throws -> Bool) -> Publishers.Optional<Publishers.Sequence<Elements, Failure>.Output, Error>
|
||||
|
||||
public func contains(where predicate: (Publishers.Sequence<Elements, Failure>.Output) -> Bool) -> Publishers.Once<Bool, Failure>
|
||||
|
||||
public func tryContains(where predicate: (Publishers.Sequence<Elements, Failure>.Output) throws -> Bool) -> Publishers.Once<Bool, Error>
|
||||
|
||||
public func drop(while predicate: (Elements.Element) -> Bool) -> Publishers.Sequence<DropWhileSequence<Elements>, Failure>
|
||||
|
||||
public func dropFirst(_ count: Int = 1) -> Publishers.Sequence<DropFirstSequence<Elements>, Failure>
|
||||
|
||||
public func first(where predicate: (Publishers.Sequence<Elements, Failure>.Output) -> Bool) -> Publishers.Optional<Publishers.Sequence<Elements, Failure>.Output, Failure>
|
||||
|
||||
public func tryFirst(where predicate: (Publishers.Sequence<Elements, Failure>.Output) throws -> Bool) -> Publishers.Optional<Publishers.Sequence<Elements, Failure>.Output, Error>
|
||||
|
||||
public func filter(_ isIncluded: (Publishers.Sequence<Elements, Failure>.Output) -> Bool) -> Publishers.Sequence<[Publishers.Sequence<Elements, Failure>.Output], Failure>
|
||||
|
||||
public func ignoreOutput() -> Publishers.Empty<Publishers.Sequence<Elements, Failure>.Output, Failure>
|
||||
|
||||
public func map<T>(_ transform: (Elements.Element) -> T) -> Publishers.Sequence<[T], Failure>
|
||||
|
||||
public func prefix(_ maxLength: Int) -> Publishers.Sequence<PrefixSequence<Elements>, Failure>
|
||||
|
||||
public func prefix(while predicate: (Elements.Element) -> Bool) -> Publishers.Sequence<[Elements.Element], Failure>
|
||||
|
||||
public func reduce<T>(_ initialResult: T, _ nextPartialResult: @escaping (T, Publishers.Sequence<Elements, Failure>.Output) -> T) -> Publishers.Once<T, Failure>
|
||||
|
||||
public func tryReduce<T>(_ initialResult: T, _ nextPartialResult: @escaping (T, Publishers.Sequence<Elements, Failure>.Output) throws -> T) -> Publishers.Once<T, Error>
|
||||
|
||||
public func replaceNil<T>(with output: T) -> Publishers.Sequence<[Publishers.Sequence<Elements, Failure>.Output], Failure> where Elements.Element == T?
|
||||
|
||||
public func scan<T>(_ initialResult: T, _ nextPartialResult: @escaping (T, Publishers.Sequence<Elements, Failure>.Output) -> T) -> Publishers.Sequence<[T], Failure>
|
||||
|
||||
public func setFailureType<E>(to error: E.Type) -> Publishers.Sequence<Elements, E> where E : Error
|
||||
}
|
||||
|
||||
extension Publishers.Sequence where Elements.Element : Equatable {
|
||||
|
||||
public func removeDuplicates() -> Publishers.Sequence<[Publishers.Sequence<Elements, Failure>.Output], Failure>
|
||||
|
||||
public func contains(_ output: Elements.Element) -> Publishers.Once<Bool, Failure>
|
||||
}
|
||||
|
||||
extension Publishers.Sequence where Elements.Element : Comparable {
|
||||
|
||||
public func min() -> Publishers.Optional<Elements.Element, Failure>
|
||||
|
||||
public func max() -> Publishers.Optional<Elements.Element, Failure>
|
||||
}
|
||||
|
||||
extension Publishers.Sequence where Elements : Collection {
|
||||
|
||||
public func first() -> Publishers.Optional<Elements.Element, Failure>
|
||||
}
|
||||
|
||||
extension Publishers.Sequence where Elements : Collection {
|
||||
|
||||
public func count() -> Publishers.Once<Int, Failure>
|
||||
}
|
||||
|
||||
extension Publishers.Sequence where Elements : Collection {
|
||||
|
||||
public func output(at index: Elements.Index) -> Publishers.Optional<Publishers.Sequence<Elements, Failure>.Output, Failure>
|
||||
|
||||
public func output(in range: Range<Elements.Index>) -> Publishers.Sequence<[Publishers.Sequence<Elements, Failure>.Output], Failure>
|
||||
}
|
||||
|
||||
extension Publishers.Sequence where Elements : BidirectionalCollection {
|
||||
|
||||
public func last() -> Publishers.Optional<Publishers.Sequence<Elements, Failure>.Output, Failure>
|
||||
|
||||
public func last(where predicate: (Publishers.Sequence<Elements, Failure>.Output) -> Bool) -> Publishers.Optional<Publishers.Sequence<Elements, Failure>.Output, Failure>
|
||||
|
||||
public func tryLast(where predicate: (Publishers.Sequence<Elements, Failure>.Output) throws -> Bool) -> Publishers.Optional<Publishers.Sequence<Elements, Failure>.Output, Error>
|
||||
}
|
||||
|
||||
extension Publishers.Sequence where Elements : RandomAccessCollection {
|
||||
|
||||
public func output(at index: Elements.Index) -> Publishers.Optional<Publishers.Sequence<Elements, Failure>.Output, Failure>
|
||||
|
||||
public func output(in range: Range<Elements.Index>) -> Publishers.Sequence<[Publishers.Sequence<Elements, Failure>.Output], Failure>
|
||||
}
|
||||
|
||||
extension Publishers.Sequence where Elements : RandomAccessCollection {
|
||||
|
||||
public func count() -> Publishers.Optional<Int, Failure>
|
||||
}
|
||||
|
||||
extension Publishers.Sequence where Elements : RangeReplaceableCollection {
|
||||
|
||||
public func prepend(_ elements: Publishers.Sequence<Elements, Failure>.Output...) -> Publishers.Sequence<Elements, Failure>
|
||||
|
||||
public func prepend<S>(_ elements: S) -> Publishers.Sequence<Elements, Failure> where S : Sequence, Elements.Element == S.Element
|
||||
|
||||
public func prepend(_ publisher: Publishers.Sequence<Elements, Failure>) -> Publishers.Sequence<Elements, Failure>
|
||||
|
||||
public func append(_ elements: Publishers.Sequence<Elements, Failure>.Output...) -> Publishers.Sequence<Elements, Failure>
|
||||
|
||||
public func append<S>(_ elements: S) -> Publishers.Sequence<Elements, Failure> where S : Sequence, Elements.Element == S.Element
|
||||
|
||||
public func append(_ publisher: Publishers.Sequence<Elements, Failure>) -> Publishers.Sequence<Elements, Failure>
|
||||
}
|
||||
|
||||
extension Publishers.Zip : Equatable where A : Equatable, B : Equatable {
|
||||
|
||||
/// Returns a Boolean value that indicates whether two publishers are equivalent.
|
||||
|
||||
@@ -110,6 +110,296 @@ extension Publishers.Sequence {
|
||||
|
||||
extension Publishers.Sequence: Equatable where Elements: Equatable {}
|
||||
|
||||
extension Publishers.Sequence {
|
||||
|
||||
public func allSatisfy(
|
||||
_ predicate: (Elements.Element) -> Bool
|
||||
) -> Publishers.Once<Bool, Failure> {
|
||||
return .init(sequence.allSatisfy(predicate))
|
||||
}
|
||||
|
||||
public func tryAllSatisfy(
|
||||
_ predicate: (Elements.Element) throws -> Bool
|
||||
) -> Publishers.Once<Bool, Error> {
|
||||
return .init(Result { try sequence.allSatisfy(predicate) })
|
||||
}
|
||||
|
||||
public func collect() -> Publishers.Once<[Elements.Element], Failure> {
|
||||
return .init(Array(sequence))
|
||||
}
|
||||
|
||||
public func compactMap<ElementOfResult>(
|
||||
_ transform: (Elements.Element) -> ElementOfResult?
|
||||
) -> Publishers.Sequence<[ElementOfResult], Failure> {
|
||||
return .init(sequence: sequence.compactMap(transform))
|
||||
}
|
||||
|
||||
public func min(
|
||||
by areInIncreasingOrder: (Elements.Element, Elements.Element) -> Bool
|
||||
) -> Publishers.Optional<Elements.Element, Failure> {
|
||||
return .init(sequence.min(by: areInIncreasingOrder))
|
||||
}
|
||||
|
||||
public func tryMin(
|
||||
by areInIncreasingOrder: (Elements.Element, Elements.Element) throws -> Bool
|
||||
) -> Publishers.Optional<Elements.Element, Error> {
|
||||
return .init(Result { try sequence.min(by: areInIncreasingOrder) })
|
||||
}
|
||||
|
||||
public func max(
|
||||
by areInIncreasingOrder: (Elements.Element, Elements.Element) -> Bool
|
||||
) -> Publishers.Optional<Elements.Element, Failure> {
|
||||
return .init(sequence.max(by: areInIncreasingOrder))
|
||||
}
|
||||
|
||||
public func tryMax(
|
||||
by areInIncreasingOrder: (Elements.Element, Elements.Element) throws -> Bool
|
||||
) -> Publishers.Optional<Elements.Element, Error> {
|
||||
return .init(Result { try sequence.max(by: areInIncreasingOrder) })
|
||||
}
|
||||
|
||||
public func contains(
|
||||
where predicate: (Elements.Element) -> Bool
|
||||
) -> Publishers.Once<Bool, Failure> {
|
||||
return .init(sequence.contains(where: predicate))
|
||||
}
|
||||
|
||||
public func tryContains(
|
||||
where predicate: (Elements.Element) throws -> Bool
|
||||
) -> Publishers.Once<Bool, Error> {
|
||||
return .init(Result { try sequence.contains(where: predicate) })
|
||||
}
|
||||
|
||||
public func drop(
|
||||
while predicate: (Elements.Element) -> Bool
|
||||
) -> Publishers.Sequence<DropWhileSequence<Elements>, Failure> {
|
||||
return .init(sequence: sequence.drop(while: predicate))
|
||||
}
|
||||
|
||||
public func dropFirst(
|
||||
_ count: Int = 1
|
||||
) -> Publishers.Sequence<DropFirstSequence<Elements>, Failure> {
|
||||
return .init(sequence: sequence.dropFirst(count))
|
||||
}
|
||||
|
||||
public func first(
|
||||
where predicate: (Elements.Element) -> Bool
|
||||
) -> Publishers.Optional<Elements.Element, Failure> {
|
||||
return .init(sequence.first(where: predicate))
|
||||
}
|
||||
|
||||
public func tryFirst(
|
||||
where predicate: (Elements.Element) throws -> Bool
|
||||
) -> Publishers.Optional<Elements.Element, Error> {
|
||||
return .init(Result { try sequence.first(where: predicate) })
|
||||
}
|
||||
|
||||
public func filter(
|
||||
_ isIncluded: (Elements.Element) -> Bool
|
||||
) -> Publishers.Sequence<[Elements.Element], Failure> {
|
||||
return .init(sequence: sequence.filter(isIncluded))
|
||||
}
|
||||
|
||||
public func ignoreOutput() -> Publishers.Empty<Elements.Element, Failure> {
|
||||
return .init(completeImmediately: true)
|
||||
}
|
||||
|
||||
public func map<ElementOfResult>(
|
||||
_ transform: (Elements.Element) -> ElementOfResult
|
||||
) -> Publishers.Sequence<[ElementOfResult], Failure> {
|
||||
return .init(sequence: sequence.map(transform))
|
||||
}
|
||||
|
||||
public func prefix(
|
||||
_ maxLength: Int
|
||||
) -> Publishers.Sequence<PrefixSequence<Elements>, Failure> {
|
||||
return .init(sequence: sequence.prefix(maxLength))
|
||||
}
|
||||
|
||||
public func prefix(
|
||||
while predicate: (Elements.Element) -> Bool
|
||||
) -> Publishers.Sequence<[Elements.Element], Failure> {
|
||||
return .init(sequence: sequence.prefix(while: predicate))
|
||||
}
|
||||
|
||||
public func reduce<Accumulator>(
|
||||
_ initialResult: Accumulator,
|
||||
_ nextPartialResult: @escaping (Accumulator, Elements.Element) -> Accumulator
|
||||
) -> Publishers.Once<Accumulator, Failure> {
|
||||
return .init(sequence.reduce(initialResult, nextPartialResult))
|
||||
}
|
||||
|
||||
public func tryReduce<Accumulator>(
|
||||
_ initialResult: Accumulator,
|
||||
_ nextPartialResult:
|
||||
@escaping (Accumulator, Elements.Element) throws -> Accumulator
|
||||
) -> Publishers.Once<Accumulator, Error> {
|
||||
return .init(Result { try sequence.reduce(initialResult, nextPartialResult) })
|
||||
}
|
||||
|
||||
public func replaceNil<ElementOfResult>(
|
||||
with output: ElementOfResult
|
||||
) -> Publishers.Sequence<[Elements.Element], Failure>
|
||||
where Elements.Element == ElementOfResult?
|
||||
{
|
||||
return .init(sequence: sequence.map { $0 ?? output })
|
||||
}
|
||||
|
||||
public func scan<ElementOfResult>(
|
||||
_ initialResult: ElementOfResult,
|
||||
_ nextPartialResult:
|
||||
@escaping (ElementOfResult, Elements.Element) -> ElementOfResult
|
||||
) -> Publishers.Sequence<[ElementOfResult], Failure> {
|
||||
var accumulator = initialResult
|
||||
return .init(sequence: sequence.map {
|
||||
accumulator = nextPartialResult(accumulator, $0)
|
||||
return accumulator
|
||||
})
|
||||
}
|
||||
|
||||
public func setFailureType<NewFailure: Error>(
|
||||
to error: NewFailure.Type
|
||||
) -> Publishers.Sequence<Elements, NewFailure> {
|
||||
return .init(sequence: sequence)
|
||||
}
|
||||
}
|
||||
|
||||
extension Publishers.Sequence where Elements.Element: Equatable {
|
||||
|
||||
public func removeDuplicates() -> Publishers.Sequence<[Elements.Element], Failure> {
|
||||
var previous: Elements.Element?
|
||||
var result = [Elements.Element]()
|
||||
for element in sequence where element != previous {
|
||||
result.append(element)
|
||||
previous = element
|
||||
}
|
||||
return .init(sequence: result)
|
||||
}
|
||||
|
||||
public func contains(_ output: Elements.Element) -> Publishers.Once<Bool, Failure> {
|
||||
return .init(sequence.contains(output))
|
||||
}
|
||||
}
|
||||
|
||||
extension Publishers.Sequence where Elements.Element: Comparable {
|
||||
|
||||
public func min() -> Publishers.Optional<Elements.Element, Failure> {
|
||||
return .init(sequence.min())
|
||||
}
|
||||
|
||||
public func max() -> Publishers.Optional<Elements.Element, Failure> {
|
||||
return .init(sequence.max())
|
||||
}
|
||||
}
|
||||
|
||||
extension Publishers.Sequence where Elements: Collection {
|
||||
|
||||
public func first() -> Publishers.Optional<Elements.Element, Failure> {
|
||||
return .init(sequence.first)
|
||||
}
|
||||
|
||||
public func count() -> Publishers.Once<Int, Failure> {
|
||||
return .init(sequence.count)
|
||||
}
|
||||
|
||||
public func output(
|
||||
at index: Elements.Index
|
||||
) -> Publishers.Optional<Elements.Element, Failure> {
|
||||
return .init(sequence.indices.contains(index) ? sequence[index] : nil)
|
||||
}
|
||||
|
||||
public func output(
|
||||
in range: Range<Elements.Index>
|
||||
) -> Publishers.Sequence<[Elements.Element], Failure> {
|
||||
return .init(sequence: Array(sequence[range]))
|
||||
}
|
||||
}
|
||||
|
||||
extension Publishers.Sequence where Elements: BidirectionalCollection {
|
||||
|
||||
public func last() -> Publishers.Optional<Elements.Element, Failure> {
|
||||
return .init(sequence.last)
|
||||
}
|
||||
|
||||
public func last(
|
||||
where predicate: (Elements.Element) -> Bool
|
||||
) -> Publishers.Optional<Elements.Element, Failure> {
|
||||
return .init(sequence.last(where: predicate))
|
||||
}
|
||||
|
||||
public func tryLast(
|
||||
where predicate: (Elements.Element) throws -> Bool
|
||||
) -> Publishers.Optional<Elements.Element, Error> {
|
||||
return .init(Result { try sequence.last(where: predicate) })
|
||||
}
|
||||
}
|
||||
|
||||
extension Publishers.Sequence where Elements: RandomAccessCollection {
|
||||
|
||||
public func output(
|
||||
at index: Elements.Index
|
||||
) -> Publishers.Optional<Elements.Element, Failure> {
|
||||
return .init(sequence.indices.contains(index) ? sequence[index] : nil)
|
||||
}
|
||||
|
||||
public func output(
|
||||
in range: Range<Elements.Index>
|
||||
) -> Publishers.Sequence<[Elements.Element], Failure> {
|
||||
return .init(sequence: Array(sequence[range]))
|
||||
}
|
||||
|
||||
public func count() -> Publishers.Optional<Int, Failure> {
|
||||
return .init(sequence.count)
|
||||
}
|
||||
}
|
||||
|
||||
extension Publishers.Sequence where Elements: RangeReplaceableCollection {
|
||||
|
||||
public func prepend(
|
||||
_ elements: Elements.Element...
|
||||
) -> Publishers.Sequence<Elements, Failure> {
|
||||
return prepend(elements)
|
||||
}
|
||||
|
||||
public func prepend<OtherSequence: Sequence>(
|
||||
_ elements: OtherSequence
|
||||
) -> Publishers.Sequence<Elements, Failure>
|
||||
where OtherSequence.Element == Elements.Element
|
||||
{
|
||||
var result = Elements(elements)
|
||||
result.append(contentsOf: sequence)
|
||||
return .init(sequence: result)
|
||||
}
|
||||
|
||||
public func prepend(
|
||||
_ publisher: Publishers.Sequence<Elements, Failure>
|
||||
) -> Publishers.Sequence<Elements, Failure> {
|
||||
return prepend(publisher.sequence)
|
||||
}
|
||||
|
||||
public func append(
|
||||
_ elements: Publishers.Sequence<Elements, Failure>.Output...
|
||||
) -> Publishers.Sequence<Elements, Failure> {
|
||||
return append(elements)
|
||||
}
|
||||
|
||||
public func append<OtherSequence: Sequence>(
|
||||
_ elements: OtherSequence
|
||||
) -> Publishers.Sequence<Elements, Failure>
|
||||
where OtherSequence.Element == Elements.Element
|
||||
{
|
||||
var result = sequence
|
||||
result.append(contentsOf: elements)
|
||||
return .init(sequence: result)
|
||||
}
|
||||
|
||||
public func append(
|
||||
_ publisher: Publishers.Sequence<Elements, Failure>
|
||||
) -> Publishers.Sequence<Elements, Failure> {
|
||||
return append(publisher.sequence)
|
||||
}
|
||||
}
|
||||
|
||||
extension Sequence {
|
||||
|
||||
public func publisher() -> Publishers.Sequence<Self, Never> {
|
||||
|
||||
Reference in New Issue
Block a user