Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example-function-in-playground #19

Merged
merged 2 commits into from
May 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 71 additions & 51 deletions OSXPlayground.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ Creating an Observable is one thing, but if nothing subscribes to the observable
`empty` creates an observable that contains no objects. The only message it sends is the `.Completed` message.
*/

let emptyObservable: Observable<Int> = empty()

let emptySubscriber = emptyObservable >- subscribe { event in
switch event {
case .Next(let box):
println("\(box.value)")
case .Completed:
println("completed")
case .Error(let error):
println("\(error)")
example("Empty observable") {
let emptyObservable: Observable<Int> = empty()

let emptySubscriber = emptyObservable >- subscribe { event in
switch event {
case .Next(let box):
println("\(box.value)")
case .Completed:
println("completed")
case .Error(let error):
println("\(error)")
}
}
}

Expand All @@ -41,28 +43,33 @@ As you can see, no values are ever sent to the subscriber of an empty observable
`never` creates an observable that contains no objects and never completes or errors out.
*/

let neverObservable: Observable<String> = never()

let neverSubscriber = neverObservable >- subscribe { _ in
println("This block is never called.")
example("Never observable") {
let neverObservable: Observable<String> = never()

let neverSubscriber = neverObservable >- subscribe { _ in
println("This block is never called.")
}
}

/*:
### returnElement/just
These two functions behave identically. They send two messages to subscribers. The first message is the value and the second message is `.Complete`.
*/

let oneObservable = just(32)

let oneObservableSubscriber = oneObservable >- subscribe { event in
switch event {
case .Next(let box):
println("\(box.value)")
case .Completed:
println("completed")
case .Error(let error):
println("\(error)")
}
example("returnElement/just") {
let oneObservable = just(32)

let oneObservableSubscriber = oneObservable
>- subscribe { event in
switch event {
case .Next(let box):
println("\(box.value)")
case .Completed:
println("completed")
case .Error(let error):
println("\(error)")
}
}
}

/*:
Expand All @@ -74,16 +81,19 @@ Here we see that the `.Next` event is sent just once, then the `.Completed` even
Now we are getting to some more interesting ways to create an Observable. This function creates an observable that produces a number of values before completing.
*/

let multipleObservable = returnElements(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

let multipleObservableSubscriber = multipleObservable >- subscribe { event in
switch event {
case .Next(let box):
println("\(box.value)")
case .Completed:
println("completed")
case .Error(let error):
println("\(error)")
example("returnElements") {
let multipleObservable/* : Observable<Int> */ = returnElements(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

let multipleObservableSubscriber = multipleObservable
>- subscribe { event in
switch event {
case .Next(let box):
println("\(box.value)")
case .Completed:
println("completed")
case .Error(let error):
println("\(error)")
}
}
}

Expand All @@ -104,8 +114,11 @@ Take some time and search for code matching `-> Observable` in the RxCocoa frame
Up to this point, I have only used the `subscribe` method to listen to Observables, but there are several others.
*/

let nextOnlySubscriber = multipleObservable >- subscribeNext { value in
println("\(value)")
example("subscribeNext") {
let nextOnlySubscriber = returnElements(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
>- subscribeNext { value in
println("\(value)")
}
}

/*:
Expand All @@ -122,25 +135,30 @@ Now that you understand how to create Observables and subscribe to them. Let's l
The most common way to reduce a sequence is to apply a filter to it and the most generic of these is `where` or `filter`. You will see in the code below that the messages containing odd numbers are being removed so the subscriber wont see them.
*/

var onlyEvensSubscriber = multipleObservable
>- filter {
$0 % 2 == 0
}
>- subscribeNext { value in
println("\(value)")
example("filter") {
let onlyEvensSubscriber = returnElements(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
>- filter {
$0 % 2 == 0
}
>- subscribeNext { value in
println("\(value)")
}
}

/*:
### distinctUntilChanged
This filter tracks the last value emitted and removes like values. This function is good for reducing noise in a sequence.
*/

let distinctUntilChangedSubscriber = returnElements(1, 2, 3, 1, 1, 4)
>- distinctUntilChanged
>- subscribeNext { value in
println("\(value)")
example("distinctUntilChanged") {
let distinctUntilChangedSubscriber = returnElements(1, 2, 3, 1, 1, 4)
>- distinctUntilChanged
>- subscribeNext { value in
println("\(value)")
}
}


/*:
In the example above, the values 1, 2, 3, 1, 4 will be printed. The extra 1 will be filtered out.
There are several different versions of `distinctUntilChanged`. Have a look in the file Observable+Single.swift to review them.
Expand All @@ -153,10 +171,12 @@ There are several different versions of `distinctUntilChanged`. Have a look in t
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.
*/

let aggregateSubscriber = multipleObservable
>- aggregate(0, +)
>- subscribeNext { value in
println("\(value)")
example("aggregate") {
let aggregateSubscriber = returnElements(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
>- aggregate(0, +)
>- subscribeNext { value in
println("\(value)")
}
}

/*:
Expand Down
9 changes: 6 additions & 3 deletions OSXPlayground.playground/Sources/SupportCode.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//
// This file (and all other Swift source files in the Sources directory of this playground) will be precompiled into a framework which is automatically made available to OSXPlayground.playground.
//


public func example(description: String, action: () -> ()) {
println("\n--- \(description) example ---")
action()
}