Skip to content
Permalink
Browse files

Add compact map for shared sequences, single and maybe

  • Loading branch information...
gringoireDM authored and kzaher committed May 16, 2019
1 parent 7929377 commit 686c5904f484566e8638a749f34ca37752f2f75d
@@ -25,6 +25,30 @@ extension SharedSequenceConvertibleType {
}
}

// MARK: compactMap
extension SharedSequenceConvertibleType {

/**
Projects each element of an observable sequence into an optional form and filters all optional results.
Equivalent to:
func compactMap<Result>(_ transform: @escaping (Self.E) -> Result?) -> SharedSequence<SharingStrategy, Result> {
return self.map(transform).filter { $0 != nil }.map { $0! }
}
- parameter transform: A transform function to apply to each source element and which returns an element or nil.
- returns: An observable sequence whose elements are the result of filtering the transform function for each element of the source.
*/
public func compactMap<Result>(_ selector: @escaping (Element) -> Result?) -> SharedSequence<SharingStrategy, Result> {
let source = self
.asObservable()
.compactMap(selector)
return SharedSequence<SharingStrategy, Result>(source)
}
}

// MARK: filter
extension SharedSequenceConvertibleType {
/**
@@ -238,6 +238,18 @@ extension PrimitiveSequenceType where Trait == MaybeTrait {
return Maybe(raw: self.primitiveSequence.source.map(transform))
}

/**
Projects each element of an observable sequence into an optional form and filters all optional results.
- parameter transform: A transform function to apply to each source element.
- returns: An observable sequence whose elements are the result of filtering the transform function for each element of the source.
*/
public func compactMap<Result>(_ transform: @escaping (Element) throws -> Result?)
-> Maybe<Result> {
return Maybe(raw: self.primitiveSequence.source.compactMap(transform))
}

/**
Projects each element of an observable sequence to an observable sequence and merges the resulting observable sequences into one observable sequence.
@@ -213,6 +213,18 @@ extension PrimitiveSequenceType where Trait == SingleTrait {
return Single(raw: self.primitiveSequence.source.map(transform))
}

/**
Projects each element of an observable sequence into an optional form and filters all optional results.
- parameter transform: A transform function to apply to each source element.
- returns: An observable sequence whose elements are the result of filtering the transform function for each element of the source.
*/
public func compactMap<Result>(_ transform: @escaping (Element) throws -> Result?)
-> Maybe<Result> {
return Maybe(raw: self.primitiveSequence.source.compactMap(transform))
}

/**
Projects each element of an observable sequence to an observable sequence and merges the resulting observable sequences into one observable sequence.

0 comments on commit 686c590

Please sign in to comment.
You can’t perform that action at this time.