Skip to content

Commit

Permalink
Add compact map for shared sequences, single and maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
gringoireDM authored and kzaher committed May 29, 2019
1 parent 7929377 commit 686c590
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
24 changes: 24 additions & 0 deletions RxCocoa/Traits/SharedSequence/SharedSequence+Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down
12 changes: 12 additions & 0 deletions RxSwift/Traits/Maybe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions RxSwift/Traits/Single.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 686c590

Please sign in to comment.