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

//
// count.swift
// RxSwiftExt
//
// Created by Fred on 06/11/2018.
// Copyright © 2018 RxSwift Community. All rights reserved.
//
import Foundation
import RxSwift
extension Observable {
/**
Count the number of items emitted by an Observable
- seealso: [count operator on reactivex.io](http://reactivex.io/documentation/operators/count.html)
- 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.
*/
public func count() -> Observable<Int> {
return reduce(0) { count, _ in count + 1 }
}
/**
Count the number of items emitted by an Observable
- seealso: [count operator on reactivex.io](http://reactivex.io/documentation/operators/count.html)
- parameter predicate: predicate determines what elements to be counted.
- 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.
*/
public func count(_ predicate: @escaping (Element) throws -> Bool) -> Observable<Int> {
return filter(predicate).count()
}
}