Beruflich Dokumente
Kultur Dokumente
Programming
By James Pang
Imperative Programming
vs
Reactive Programming
Imperative Programming
Concerned with HOW to do something to achieve
WHAT I want
Imperative Programming
Concerned with HOW to do something to achieve
WHAT I want
var
var
var
x =
z =
x
y
z
5
x
= 3
= 4
= x + y //7
+ y //9
Reactive Programming
Concerned with WHAT I want, keeping the HOW
abstracted away
Reactive Programming
Concerned with WHAT I want, keeping the HOW
abstracted away
var
var
var
x =
x
y
z
5
= 3
= 4
= x + y // 7
// z now equals 9
Reactive Programming
Concerned with WHAT I want, keeping the how
abstracted away
var
var
var
x =
x
y
z
5
= 3
= 4
= x + y // 7
// z now equals 9
WHY?
STATE IS HARD
ASYNC IS
HARD
State example
var array = [1, 2, 3]
//We want [2, 3, 4]
State example
var array = [1, 2, 3]
var newArray: [Int] = []
for var i = 0; i < array.count; i++ {
newArray.append(array[i] + 1)
}
newArray // [2, 3, 4]
Stateless example
var array = [1, 2, 3]
var newArray = array.map { x in return x + 1 } // [2, 3, 4]
State example
State example
State example
State example
var
var
var
var
State example
var
var
var
var
A.K.A
Combinatorial Explosion
1
Array
Examples
Array Example
var array = [1, 2, 3, 4, 5, 6]
//We want //[2, 4, 6]
Array Example
var array = [1, 2, 3, 4, 5, 6]
var newArray = array.filter { x in return x % 2 == 0 } // [2, 4, 6]
Array Example
var array = [1, 2, 3, 4]
//We want 10
Array Example
var array = [1, 2, 3, 4]
var sum = array.reduce(0, combine: {
previousValue, currentElement in
return previousValue + currentElement
}) // 10
Array Example
var nestedArray = [[1, 2, 3], [4, 5, 6]]
//We want [2, 3, 4, 5, 6, 7]
Array Example
var nestedArray = [[1, 2, 3], [4, 5, 6]]
var flattendArray = nestedArray.flatMap {
element in
return element.map { innerElement in
return innerElement + 1
}
} // [2, 3, 4, 5, 6, 7]
ASYNCHRONOUS
EVENTS AS ARRAYS
OVER TIME
Examples
[click...]
... = arbitrary amount of time
Examples
[click...click...]
... = arbitrary amount of time
Examples
[click...click...click...]
... = arbitrary amount of time
Examples
[click...click...click...click...]
... = arbitrary amount of time
Examples
[click...click...click...click...click...]
... = arbitrary amount of time
Examples
[click...click...click...click...click...]
[h...]
... = arbitrary amount of time
Examples
[click...click...click...click...click...]
[h...he...]
... = arbitrary amount of time
Examples
[click...click...click...click...click...]
[h...he...hel...]
... = arbitrary amount of time
Examples
[click...click...click...click...click...]
[h...he...hel...hell]
... = arbitrary amount of time
Examples
[click...click...click...click...click...]
[h...he...hel...hell...hello]
... = arbitrary amount of time
Observable
Observable
[h...]
Observable
[h...he...]
Observable
[h...he...hel...]
Observable
[h...he...hel...hell...]
Observable
[h...he...hel...hell...hello...]
Observable
[h...he...hel...hell...hello...onComplete]
OR
[h...he...hel...hell...hello...onError]
Reactive Extensions
An API for asynchronous programming with
observable streams
reactivex.io website
RxJava, RxJS, Rx.NET, RxSwift + more
EXAMPLES
EXAMPLES
Observable.just(1)
EXAMPLES
Observable.just(1)
.subscribeNext { nextValue in
print(nextValue)
}
// 1
EXAMPLES
let subscription = Observable.just(1)
.subscribeNext { nextValue in
print(nextValue)
}
// 1
subscription.dispose()
FORM
VALIDATION
EXAMPLE
AUTO
COMPLETE
EXAMPLE
});
});
return AnonymousDisposable {
API.cancel();
}
return AnonymousDisposable {
API.cancel();
}
MULTIPLE API
EXAMPLE
Unified API
across
languagues
Links
http://rxmarbles.com/
https://www.youtube.com/watch?v=XE692Clb5LU
http://reactivex.io/learnrx/
https://gist.github.com/staltz/868e7e9bc2a7b8c1f754
Thank you!