0001 // 0002 // Observable+Aggregate.swift 0003 // Rx 0004 // 0005 // Created by Krunoslav Zaher on 3/22/15. 0006 // Copyright © 2015 Krunoslav Zaher. All rights reserved. 0007 // 0008 0009 import Foundation 0010 0011 // MARK: reduce 0012 0013 extension ObservableType { 0014 0015 /** 0016 Applies an `accumulator` function over an observable sequence, returning the result of the aggregation as a single element in the result sequence. The specified `seed` value is used as the initial accumulator value. 0017 0018 For aggregation behavior with incremental intermediate results, see `scan`. 0019 0020 - seealso: [reduce operator on reactivex.io](http://reactivex.io/documentation/operators/reduce.html) 0021 0022 - parameter seed: The initial accumulator value. 0023 - parameter accumulator: A accumulator function to be invoked on each element. 0024 - parameter mapResult: A function to transform the final accumulator value into the result value. 0025 - returns: An observable sequence containing a single element with the final accumulator value. 0026 */ 0027 @warn_unused_result(message="http://git.io/rxs.uo") 0028 public func reduce<A, R>(seed: A, accumulator: (A, E) throws -> A, mapResult: (A) throws -> R) 0029 -> Observable<R> { 0030 return Reduce(source: self.asObservable(), seed: seed, accumulator: accumulator, mapResult: mapResult) 0031 } 0032 0033 /** 0034 Applies an `accumulator` function over an observable sequence, returning the result of the aggregation as a single element in the result sequence. The specified `seed` value is used as the initial accumulator value. 0035 0036 For aggregation behavior with incremental intermediate results, see `scan`. 0037 0038 - seealso: [reduce operator on reactivex.io](http://reactivex.io/documentation/operators/reduce.html) 0039 0040 - parameter seed: The initial accumulator value. 0041 - parameter accumulator: A accumulator function to be invoked on each element. 0042 - returns: An observable sequence containing a single element with the final accumulator value. 0043 */ 0044 @warn_unused_result(message="http://git.io/rxs.uo") 0045 public func reduce<A>(seed: A, accumulator: (A, E) throws -> A) 0046 -> Observable<A> { 0047 return Reduce(source: self.asObservable(), seed: seed, accumulator: accumulator, mapResult: { $0 }) 0048 } 0049 0050 /** 0051 Converts an Observable into another Observable that emits the whole sequence as a single array and then terminates. 0052 0053 For aggregation behavior see `reduce`. 0054 0055 - seealso: [toArray operator on reactivex.io](http://reactivex.io/documentation/operators/to.html) 0056 0057 - returns: An observable sequence containing all the emitted elements as array. 0058 */ 0059 @warn_unused_result(message="http://git.io/rxs.uo") 0060 public func toArray() 0061 -> Observable<[E]> { 0062 return ToArray(source: self.asObservable()) 0063 } 0064 } 0065