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.

31 lines
1.2 KiB

5 years ago
  1. //
  2. // count.swift
  3. // RxSwiftExt-iOS
  4. //
  5. // Created by Fred on 06/11/2018.
  6. // Copyright © 2018 RxSwiftCommunity. All rights reserved.
  7. //
  8. import Foundation
  9. import RxSwift
  10. extension Observable {
  11. /**
  12. Count the number of items emitted by an Observable
  13. - seealso: [count operator on reactivex.io](http://reactivex.io/documentation/operators/count.html)
  14. - returns: An Observable sequence containing a value that represents how many elements in the specified observable sequence satisfy a condition if provided, else the count of items.
  15. */
  16. public func count() -> Observable<Int> {
  17. return reduce(0) { count, _ in count + 1 }
  18. }
  19. /**
  20. Count the number of items emitted by an Observable
  21. - seealso: [count operator on reactivex.io](http://reactivex.io/documentation/operators/count.html)
  22. - parameter predicate: predicate determines what elements to be counted.
  23. - returns: An Observable sequence containing a value that represents how many elements in the specified observable sequence satisfy a condition if provided, else the count of items.
  24. */
  25. public func count(_ predicate: @escaping (Element) throws -> Bool) -> Observable<Int> {
  26. return filter(predicate).count()
  27. }
  28. }