From 8b63f25fe1a25b7d78fd3ec8f2571f43f0132d01 Mon Sep 17 00:00:00 2001 From: Scott Gardner Date: Fri, 13 May 2016 15:46:04 -0500 Subject: [PATCH] Update Mathematical and Aggregate Operators --- .../Contents.swift | 4 +- .../Contents.swift | 116 ++++++++---------- .../Contents.swift | 2 +- 3 files changed, 55 insertions(+), 67 deletions(-) diff --git a/Rx.playground/Pages/Creating_and_Subscribing_to_Observables.xcplaygroundpage/Contents.swift b/Rx.playground/Pages/Creating_and_Subscribing_to_Observables.xcplaygroundpage/Contents.swift index 5c3c8544..1e56f13a 100644 --- a/Rx.playground/Pages/Creating_and_Subscribing_to_Observables.xcplaygroundpage/Contents.swift +++ b/Rx.playground/Pages/Creating_and_Subscribing_to_Observables.xcplaygroundpage/Contents.swift @@ -113,18 +113,16 @@ example("create") { /*: ---- ## 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") { let disposeBag = DisposeBag() Observable.range(start: 1, count: 10) - .toArray() .subscribe { print($0) } .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 Creates an `Observable` sequence that emits the given element indefinitely. [More info](http://reactivex.io/documentation/operators/repeat.html) diff --git a/Rx.playground/Pages/Mathematical_and_Aggregate_Operators.xcplaygroundpage/Contents.swift b/Rx.playground/Pages/Mathematical_and_Aggregate_Operators.xcplaygroundpage/Contents.swift index d305b413..e3f8598f 100644 --- a/Rx.playground/Pages/Mathematical_and_Aggregate_Operators.xcplaygroundpage/Contents.swift +++ b/Rx.playground/Pages/Mathematical_and_Aggregate_Operators.xcplaygroundpage/Contents.swift @@ -9,73 +9,63 @@ */ import RxSwift /*: -## Mathematical and Aggregate Operators - -Operators that operate on the entire sequence of items emitted by an Observable - -*/ - -/*: -### `concat` - -Emit the emissions from two or more Observables without interleaving them. - -![](https://raw.githubusercontent.com/kzaher/rxswiftcontent/master/MarbleDiagrams/png/concat.png) - -[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) + # Mathematical and Aggregate Operators + 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) + ![](http://reactivex.io/documentation/operators/images/to.c.png) + */ +example("toArray") { + let disposeBag = DisposeBag() - // var3 is like an Observable> - let var3 = BehaviorSubject(value: var1) - - let d = var3 - .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)) + Observable.range(start: 1, count: 10) + .toArray() + .subscribe { print($0) } + .addDisposableTo(disposeBag) } - - /*: - - -### `reduce` - -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. - -![](https://raw.githubusercontent.com/kzaher/rxswiftcontent/master/MarbleDiagrams/png/reduce.png) - -[More info in reactive.io website]( http://reactivex.io/documentation/operators/reduce.html ) - -*/ + ---- + ## `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) + ![](https://raw.githubusercontent.com/kzaher/rxswiftcontent/master/MarbleDiagrams/png/reduce.png) + */ example("reduce") { - _ = Observable.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) - .reduce(0, accumulator: +) - .subscribe { - print($0) - } + let disposeBag = DisposeBag() + + Observable.of(10, 100, 1000) + .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) + ![](https://raw.githubusercontent.com/kzaher/rxswiftcontent/master/MarbleDiagrams/png/concat.png) + */ +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) diff --git a/Rx.playground/Pages/Transforming_Operators.xcplaygroundpage/Contents.swift b/Rx.playground/Pages/Transforming_Operators.xcplaygroundpage/Contents.swift index 1e970417..d1768805 100644 --- a/Rx.playground/Pages/Transforming_Operators.xcplaygroundpage/Contents.swift +++ b/Rx.playground/Pages/Transforming_Operators.xcplaygroundpage/Contents.swift @@ -61,7 +61,7 @@ example("flatMap and flatMapLatest") { /*: ---- ## `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) ![](https://raw.githubusercontent.com/kzaher/rxswiftcontent/master/MarbleDiagrams/png/scan.png) */ example("scan") {