Functional reactive
programming
Hoang Hai Hung
1
September, 2016
Outline
1.Programming paradigms
2. Functional reactive programming
3. RxSwift
4. Demo
2
What is Programming language ?
A programming language is a notation for writing programs, which are
specifications of a computation or algorithm.
3
Programming paradigms
Pascal, C Java, C++ Haskell, Lisp Prolog
Swift 4
Imperative vs Declarative
Telling the “machine” how to do
something, and as a result what
you want to happen will happen.
Imperative programming
Telling the “machine” what you
would like to happen, and let the
computer figure out how to do it.
Declarative programming
BUT,
HOW ?
5
6
Imperative vs Declarative
Imperative programming Declarative programming
let numbers = [1, 2, 3, 4, 5, 6]
let sum = reduce(numbers, 0, +)
let odds = filter(numbers, { $0 % 2 == 1})
let array = [1, 2, 3, 4, 5, 6]
var sum = 0
var odds = []
for element in array {
sum += element
if element % 2 == 1 {
odds
.append(element)
}
}
Imperative vs Declarative
7
Imperative programming Declarative programming
Imperative vs Declarative
8
Imperative way
9
Imperative way (cont.)
10
Imperative way (cont.)
Pyramid of Doom
11
Declarative way
12
Imperative vs Declarative
Stand up, walk 10 steps by your foot to the
fridge, use your hand to open the door, get
me the water, walk back 10 steps ...
Imperative programming
Honey, i wish i had a glass of cold
water now.
Declarative programming
13
Thanks, honeyYou don’t say ?
Advantages of Declarative Programming
Minimizes mutability
Reduces state side-effects
More understandable code
More scalable
14
Outline
1. Programming paradigm
2. Functional reactive programming
3. RxSwift
4. Demo
15
Functional reactive programming (FRP)
Functional reactive programming (FRP) is a programming paradigm for
reactive programming using the building blocks of functional programming
16
Functional programming
17
Functional programming is a programming paradigm that treats computation
as the evaluation of mathematical functions and avoids changing-state and
mutable data
Functional programming
18
let numbers = [1, 2, 3, 4, 5, 6]
let doubled = numbers.map($0 * $0)
let sum = doubled.reduce(0, combine: +))
Reactive programming
Reactive programming is programming with asynchronous data streams.
19
Reactive programming
C = A + B
20
Functional reactive programming
Closure/Lambdas
Pure Function
First-Class Functions
High-Order Functions
Functional
Events/Data – Driven
Push
Asynchronous
Reactive
21
https://medium.com/@jugoncalves/functional-programming-should-be-your-1-priority-for-2015-47dd4641d6b9
https://medium.com/reactive-programming/what-is-reactive-programming-bc9fa7f4a7fc
Outline
1. Programming paradigm
2. Functional reactive programming
3. RxSwift
4. Demo
22
Stream
23
A stream is a sequence of ongoing events ordered in time.
Stream
24
Signal
25
Signal is a value that changes over time.
Example
Khi người dùng thực hiện đồng thời `di chuyển` (pan) và `xoay` vật thể
(rotate), thực hiện đếm ngược từ `3`. Dừng đếm ngược khi người dùng bỏ tay
ra hoặc đếm ngược đến 0.
26
Imperative way
27https://gist.github.com/dangthaison91/ab28bf2056f6c8a7240d073d0fb1f563#file-gesture-swift
Declarative way (FRP)
28https://gist.github.com/dangthaison91/8dc2ae412a675af322a376884f3f896e#file-gesture_frp-swift
Declarative way (FRP)
29
30
31
Example
Search on GitHub
32
Imperative way
33
Declarative way (FRP)
34
Advantages of FRP
Code more concise & clear, easy to understand without context
Readability, highly express
Make Asynchronous easier
35
36
Conclusion
37
Conclusion
38
Outline
1. Programming paradigm
2. Functional reactive programming
3. RxSwift
4. Demo
39
THANK YOU !
40

Functional reactive programming