Update Mathematical and Aggregate Operators
This commit is contained in:
parent
4046c1163c
commit
8b63f25fe1
|
|
@ -113,18 +113,16 @@ example("create") {
|
||||||
/*:
|
/*:
|
||||||
----
|
----
|
||||||
## range
|
## range
|
||||||
Creates an `Observable` sequence that emits a range of sequential integers. [More info](http://reactivex.io/documentation/operators/range.html)
|
Creates an `Observable` sequence that emits a range of sequential integers and then terminates. [More info](http://reactivex.io/documentation/operators/range.html)
|
||||||
*/
|
*/
|
||||||
example("range") {
|
example("range") {
|
||||||
let disposeBag = DisposeBag()
|
let disposeBag = DisposeBag()
|
||||||
|
|
||||||
Observable.range(start: 1, count: 10)
|
Observable.range(start: 1, count: 10)
|
||||||
.toArray()
|
|
||||||
.subscribe { print($0) }
|
.subscribe { print($0) }
|
||||||
.addDisposableTo(disposeBag)
|
.addDisposableTo(disposeBag)
|
||||||
}
|
}
|
||||||
/*:
|
/*:
|
||||||
> This example also introduces using the `toArray` operator to convert an entire sequence to an array, emit that array, and then terminate.
|
|
||||||
----
|
----
|
||||||
## repeatElement
|
## repeatElement
|
||||||
Creates an `Observable` sequence that emits the given element indefinitely. [More info](http://reactivex.io/documentation/operators/repeat.html)
|
Creates an `Observable` sequence that emits the given element indefinitely. [More info](http://reactivex.io/documentation/operators/repeat.html)
|
||||||
|
|
|
||||||
|
|
@ -9,73 +9,63 @@
|
||||||
*/
|
*/
|
||||||
import RxSwift
|
import RxSwift
|
||||||
/*:
|
/*:
|
||||||
## Mathematical and Aggregate Operators
|
# Mathematical and Aggregate Operators
|
||||||
|
Operators that operate on the entire sequence of items emitted by an `Observable`.
|
||||||
Operators that operate on the entire sequence of items emitted by an Observable
|
## `toArray`
|
||||||
|
Converts an `Observable` sequence into an array, emits that array as a new single-element `Observable` sequence, and then terminates. [More info](http://reactivex.io/documentation/operators/to.html)
|
||||||
*/
|

|
||||||
|
*/
|
||||||
/*:
|
example("toArray") {
|
||||||
### `concat`
|
let disposeBag = DisposeBag()
|
||||||
|
|
||||||
Emit the emissions from two or more Observables without interleaving them.
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/concat.html )
|
|
||||||
*/
|
|
||||||
example("concat") {
|
|
||||||
let var1 = BehaviorSubject(value: 0)
|
|
||||||
let var2 = BehaviorSubject(value: 200)
|
|
||||||
|
|
||||||
// var3 is like an Observable<Observable<Int>>
|
Observable.range(start: 1, count: 10)
|
||||||
let var3 = BehaviorSubject(value: var1)
|
.toArray()
|
||||||
|
.subscribe { print($0) }
|
||||||
let d = var3
|
.addDisposableTo(disposeBag)
|
||||||
.concat()
|
|
||||||
.subscribe {
|
|
||||||
print($0)
|
|
||||||
}
|
|
||||||
|
|
||||||
var1.on(.Next(1))
|
|
||||||
var1.on(.Next(2))
|
|
||||||
var1.on(.Next(3))
|
|
||||||
var1.on(.Next(4))
|
|
||||||
|
|
||||||
var3.on(.Next(var2))
|
|
||||||
|
|
||||||
var2.on(.Next(201))
|
|
||||||
|
|
||||||
var1.on(.Next(5))
|
|
||||||
var1.on(.Next(6))
|
|
||||||
var1.on(.Next(7))
|
|
||||||
var1.on(.Completed)
|
|
||||||
|
|
||||||
var2.on(.Next(202))
|
|
||||||
var2.on(.Next(203))
|
|
||||||
var2.on(.Next(204))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*:
|
/*:
|
||||||
|
----
|
||||||
|
## `reduce`
|
||||||
### `reduce`
|
Begins with an initial seed value, and then applies an accumulator closure to all elements emitted by an `Observable` sequence, and returns the aggregate result as a single-element `Observable` sequence. [More info](http://reactivex.io/documentation/operators/reduce.html)
|
||||||
|

|
||||||
Apply a function to each item emitted by an Observable, sequentially, and emit the final value.
|
*/
|
||||||
This function will perform a function on each element in the sequence until it is completed, then send a message with the aggregate value. It works much like the Swift `reduce` function works on sequences.
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/reduce.html )
|
|
||||||
|
|
||||||
*/
|
|
||||||
example("reduce") {
|
example("reduce") {
|
||||||
_ = Observable.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
let disposeBag = DisposeBag()
|
||||||
.reduce(0, accumulator: +)
|
|
||||||
.subscribe {
|
Observable.of(10, 100, 1000)
|
||||||
print($0)
|
.reduce(1, accumulator: +)
|
||||||
}
|
.subscribeNext { print($0) }
|
||||||
|
.addDisposableTo(disposeBag)
|
||||||
|
}
|
||||||
|
/*:
|
||||||
|
----
|
||||||
|
## `concat`
|
||||||
|
Joins elements from inner `Observable` sequences of an `Observable` sequence in a sequential manner, waiting for each sequence to terminate successfully before emitting elements from the next sequence. [More info](http://reactivex.io/documentation/operators/concat.html)
|
||||||
|

|
||||||
|
*/
|
||||||
|
example("concat") {
|
||||||
|
let disposeBag = DisposeBag()
|
||||||
|
|
||||||
|
let subject1 = BehaviorSubject(value: "🍎")
|
||||||
|
let subject2 = BehaviorSubject(value: "🐶")
|
||||||
|
|
||||||
|
let variable = Variable(subject1)
|
||||||
|
|
||||||
|
variable.asObservable()
|
||||||
|
.concat()
|
||||||
|
.subscribe { print($0) }
|
||||||
|
.addDisposableTo(disposeBag)
|
||||||
|
|
||||||
|
subject1.onNext("🍐")
|
||||||
|
subject1.onNext("🍊")
|
||||||
|
|
||||||
|
variable.value = subject2
|
||||||
|
|
||||||
|
subject2.onNext("🐱")
|
||||||
|
|
||||||
|
subject1.onCompleted()
|
||||||
|
|
||||||
|
subject2.onNext("🐭")
|
||||||
}
|
}
|
||||||
|
|
||||||
//: [Next](@next) - [Table of Contents](Table_of_Contents)
|
//: [Next](@next) - [Table of Contents](Table_of_Contents)
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ example("flatMap and flatMapLatest") {
|
||||||
/*:
|
/*:
|
||||||
----
|
----
|
||||||
## `scan`
|
## `scan`
|
||||||
Begins with an initial seed value, and then applies an accumulator closure to each element emitted by an `Observable` sequence, and returns each intermediate result. [More info](http://reactivex.io/documentation/operators/scan.html)
|
Begins with an initial seed value, and then applies an accumulator closure to each element emitted by an `Observable` sequence, and returns each intermediate result as a single-element `Observable` sequence. [More info](http://reactivex.io/documentation/operators/scan.html)
|
||||||

|

|
||||||
*/
|
*/
|
||||||
example("scan") {
|
example("scan") {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue