You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
995 B

5 years ago
  1. //
  2. // partition+RxCocoa.swift
  3. // RxSwiftExt
  4. //
  5. // Created by Shai Mishali on 24/11/2018.
  6. // Copyright © 2018 RxSwift Community. All rights reserved.
  7. //
  8. import RxSwift
  9. import RxCocoa
  10. public extension SharedSequence {
  11. /**
  12. Partition a stream into two separate streams of elements that match, and don't match, the provided predicate.
  13. - parameter predicate: A predicate used to filter matching and non-matching elements.
  14. - returns: A tuple of two streams of elements that match, and don't match, the provided predicate.
  15. */
  16. func partition(_ predicate: @escaping (Element) -> Bool) -> (matches: SharedSequence<SharingStrategy, Element>,
  17. nonMatches: SharedSequence<SharingStrategy, Element>) {
  18. let stream = self.map { ($0, predicate($0)) }
  19. let hits = stream.filter { $0.1 }.map { $0.0 }
  20. let misses = stream.filter { !$0.1 }.map { $0.0 }
  21. return (hits, misses)
  22. }
  23. }