5. 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. 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)
}
}
13. 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 ?
14. Advantages of Declarative Programming
Minimizes mutability
Reduces state side-effects
More understandable code
More scalable
14
16. Functional reactive programming (FRP)
Functional reactive programming (FRP) is a programming paradigm for
reactive programming using the building blocks of functional programming
16
26. 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