Update Filtering Operators
This commit is contained in:
parent
c0bebbb795
commit
a68c07beb5
|
|
@ -9,163 +9,159 @@
|
|||
*/
|
||||
import RxSwift
|
||||
/*:
|
||||
## Filtering Operators
|
||||
|
||||
Operators that selectively emit items from a source Observable.
|
||||
*/
|
||||
|
||||
/*:
|
||||
### `filter`
|
||||
|
||||
Emit only those items from an Observable that pass a predicate test
|
||||
|
||||
# Filtering Operators
|
||||
Operators that selectively emit elements from a source `Observable` sequence.
|
||||
## `filter`
|
||||
Emits only those elements from an `Observable` sequence that meet the specified condition. [More info](http://reactivex.io/documentation/operators/filter.html)
|
||||

|
||||
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/filter.html )
|
||||
*/
|
||||
|
||||
example("filter") {
|
||||
let subscription = Observable.of("🐶","🐱","🐭","🐹","🐰","🐸","🐱","🐼","🐨","🐱","🐸","🐱","🐷","🐽","🐸","🐱","🐵")
|
||||
let disposeBag = DisposeBag()
|
||||
|
||||
Observable.of(
|
||||
"🐱", "🐰", "🐶",
|
||||
"🐸", "🐱", "🐰",
|
||||
"🐹", "🐸", "🐱")
|
||||
.filter {
|
||||
$0 == "🐱"
|
||||
}
|
||||
.subscribe {
|
||||
print($0)
|
||||
}
|
||||
.subscribeNext { print($0) }
|
||||
.addDisposableTo(disposeBag)
|
||||
}
|
||||
|
||||
|
||||
/*:
|
||||
### `distinctUntilChanged`
|
||||
|
||||
Suppress duplicate items emitted by an Observable
|
||||
|
||||
----
|
||||
## `distinctUntilChanged`
|
||||
Suppresses sequential duplicate elements emitted by an `Observable` sequence. [More info](http://reactivex.io/documentation/operators/distinct.html)
|
||||

|
||||
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/distinct.html )
|
||||
*/
|
||||
example("distinctUntilChanged") {
|
||||
let subscription = Observable.of("🐶","🐱","🐱","🐱","🐱","🐱","🐱","🐱","🐱","🐱","🐱","🐷","🐵")
|
||||
let disposeBag = DisposeBag()
|
||||
|
||||
Observable.of("🐱", "🐷", "🐱", "🐱", "🐱", "🐵", "🐱")
|
||||
.distinctUntilChanged()
|
||||
.subscribe {
|
||||
print($0)
|
||||
}
|
||||
.subscribeNext { print($0) }
|
||||
.addDisposableTo(disposeBag)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*:
|
||||
### `elementAt`
|
||||
|
||||
Emit only item n of all elemets emitted by an Observable
|
||||
|
||||
----
|
||||
## `elementAt`
|
||||
Emits only the element at the specified index of all elements emitted by an `Observable` sequence. [More info](http://reactivex.io/documentation/operators/elementat.html)
|
||||

|
||||
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/elementat.html )
|
||||
*/
|
||||
|
||||
example("elementAt") {
|
||||
let subscription = Observable.of("🔴","🔵","⚪️","㊗️")
|
||||
.elementAt(2)
|
||||
.subscribe {
|
||||
print($0)
|
||||
}
|
||||
let disposeBag = DisposeBag()
|
||||
|
||||
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
|
||||
.elementAt(3)
|
||||
.subscribeNext { print($0) }
|
||||
.addDisposableTo(disposeBag)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*:
|
||||
### `take`
|
||||
|
||||
Emit only the first n items emitted by an Observable
|
||||
|
||||

|
||||
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/take.html )
|
||||
*/
|
||||
example("take") {
|
||||
let subscription = Observable.of("🐶","🐱","🐭","🐹")
|
||||
.take(3)
|
||||
.subscribe {
|
||||
print($0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*:
|
||||
### `single` (a.k.a first)
|
||||
|
||||
Emit only the first item (or the first item that meets some condition) emitted by an Observable
|
||||
|
||||

|
||||
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/first.html )
|
||||
----
|
||||
## `single`
|
||||
Emits only the first element (or the first element that meets a condition) emitted by an `Observable` sequence. Will throw an error if the `Observable` sequence does not emit exactly one element.
|
||||
*/
|
||||
example("single") {
|
||||
let subscription = Observable.of("🐶","🐱","🐭","🐹")
|
||||
let disposeBag = DisposeBag()
|
||||
|
||||
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
|
||||
.single()
|
||||
.subscribe {
|
||||
print($0)
|
||||
}
|
||||
.subscribeNext { print($0) }
|
||||
.addDisposableTo(disposeBag)
|
||||
}
|
||||
|
||||
example("single with predicate") {
|
||||
let subscription = Observable.of("🐶","🐱","🐭","🐹")
|
||||
.single {
|
||||
$0 == "🔵"
|
||||
}
|
||||
.subscribe {
|
||||
print($0)
|
||||
}
|
||||
example("single with conditions") {
|
||||
let disposeBag = DisposeBag()
|
||||
|
||||
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
|
||||
.single { $0 == "🔵" }
|
||||
.subscribe { print($0) }
|
||||
.addDisposableTo(disposeBag)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*:
|
||||
### `takeLast`
|
||||
|
||||
Emit only the final n items emitted by an Observable
|
||||
|
||||
----
|
||||
## `take`
|
||||
Emits only the specified number of elements from the beginning of an `Observable` sequence. [More info](http://reactivex.io/documentation/operators/take.html)
|
||||

|
||||
*/
|
||||
example("take") {
|
||||
let disposeBag = DisposeBag()
|
||||
|
||||
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
|
||||
.take(3)
|
||||
.subscribeNext { print($0) }
|
||||
.addDisposableTo(disposeBag)
|
||||
}
|
||||
/*:
|
||||
----
|
||||
## `takeLast`
|
||||
Emits only the specified number of elements from the end of an `Observable` sequence. [More info](http://reactivex.io/documentation/operators/takelast.html)
|
||||

|
||||
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/takelast.html )
|
||||
*/
|
||||
example("takeLast") {
|
||||
let subscription = Observable.of("🐶","🐱","🐭","🐹")
|
||||
.takeLast(2)
|
||||
.subscribe {
|
||||
print($0)
|
||||
}
|
||||
let disposeBag = DisposeBag()
|
||||
|
||||
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
|
||||
.takeLast(3)
|
||||
.subscribeNext { print($0) }
|
||||
.addDisposableTo(disposeBag)
|
||||
}
|
||||
|
||||
|
||||
/*:
|
||||
### `skip`
|
||||
|
||||
Suppress the first n items emitted by an Observable
|
||||
|
||||
----
|
||||
## `takeWhile`
|
||||
Emits only the specified number of elements from the beginning of an `Observable` sequence that meet the specified condition. [More info](http://reactivex.io/documentation/operators/takewhile.html)
|
||||

|
||||
*/
|
||||
example("takeWhile") {
|
||||
let disposeBag = DisposeBag()
|
||||
|
||||
Observable.of(1, 2, 3, 4, 5, 6)
|
||||
.takeWhile { $0 < 4 }
|
||||
.subscribeNext { print($0) }
|
||||
.addDisposableTo(disposeBag)
|
||||
}
|
||||
/*:
|
||||
----
|
||||
## `skip`
|
||||
Suppresses emitting the specified number of elements from the beginning of an `Observable` sequence. [More info](http://reactivex.io/documentation/operators/skip.html)
|
||||

|
||||
|
||||
[More info in reactive.io website]( http://reactivex.io/documentation/operators/skip.html )
|
||||
*/
|
||||
example("skip") {
|
||||
let subscription = Observable.of("🐶","🐱","🐭","🐹")
|
||||
let disposeBag = DisposeBag()
|
||||
|
||||
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
|
||||
.skip(2)
|
||||
.subscribe {
|
||||
print($0)
|
||||
}
|
||||
.subscribeNext { print($0) }
|
||||
.addDisposableTo(disposeBag)
|
||||
}
|
||||
|
||||
|
||||
/*:
|
||||
----
|
||||
## `skipWhile`
|
||||
Suppresses emitting the elements from the beginning of an `Observable` sequence that meet the specified condition. [More info](http://reactivex.io/documentation/operators/skipwhile.html)
|
||||

|
||||
*/
|
||||
example("skipWhile") {
|
||||
let disposeBag = DisposeBag()
|
||||
|
||||
Observable.of(1, 2, 3, 4, 5, 6)
|
||||
.skipWhile { $0 < 4 }
|
||||
.subscribeNext { print($0) }
|
||||
.addDisposableTo(disposeBag)
|
||||
}
|
||||
/*:
|
||||
----
|
||||
## `skipWhileWithIndex`
|
||||
Suppresses emitting the elements from the beginning of an `Observable` sequence that meet the specified condition, and emits the remaining elements. The closure is also passed each element's index.
|
||||
*/
|
||||
example("skipWhileWithIndex") {
|
||||
let subscription = Observable.of("🐱","🐱","🐱","🐷","🐷","🐷")
|
||||
.skipWhileWithIndex { str, idx -> Bool in
|
||||
return idx < 2
|
||||
}
|
||||
.subscribe {
|
||||
print($0)
|
||||
let disposeBag = DisposeBag()
|
||||
|
||||
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
|
||||
.skipWhileWithIndex { element, index in
|
||||
index < 3
|
||||
}
|
||||
.subscribeNext { print($0) }
|
||||
.addDisposableTo(disposeBag)
|
||||
}
|
||||
|
||||
//: [Next](@next) - [Table of Contents](Table_of_Contents)
|
||||
|
|
|
|||
Loading…
Reference in New Issue