SlideShare a Scribd company logo
FRP: What does "declarative" mean
Peter Ovchinnikov
• CrossroadLabs - startups development environment
• Team lead of mobile team
• FRP + MVVM
• RE: {CODE} Lviv.
• Conferences
• Meetups
• Workshops
2
What the “declarative” really
mean
by Peter Ovchinnikov
3
Why go FRP?
4
Why go FRP
from mobile-team perspective:
• Clear code
• Avoid multithreading hell
• Reaction to changes using bindings.
• It’s easier to separate logic from UI. Controllers are thiner. MVVM!
5
That’s it! Really?
6
Why not OOP!
7
Why go FRP
from Web team:
• Clasterisation.
• State-full servers and long pooling.
• Collaboration.
• WebSockets.
• Homogeneous micro-services
8
Reactive Manifesto
• Responsive
• Resilient
• Elastic
• Message Driven
9
Reactive Manifesto
Responsive
The system responds in a timely manner if
at all possible.
10
Reactive Manifesto
Resilient
The system stays responsive in the face of
failure
11
Reactive Manifesto
Elastic
The system stays responsive under
varying workload
12
Reactive Manifesto
Message Driven
Reactive Systems rely on asynchronous message-passing to establish a
boundary between components that ensures loose coupling, isolation and
location transparency
13
Reactive Manifesto
Message Driven
14
Reactive Manifesto
Actors Model
15
What is FRP
16
What is FRP
What is FRP
What is FRP
What is FRP
What is FRP
What is FRP
What is FRP
What is FRP
What is FRP
What is FRP
FRP is:
• functional
• reactive
• asynchronous
programming
26
Signals
observable
let one = Signal(0)
one.observe { print ($0) }
one.next(1) //prints “1”
27
Signals
bindable
let one = Signal(0)
let two = Signal(0)
one.bind(to: two) // one -> two
one.bind(fromTo: two) // one <-> two
28
Signals
MVVM able
let strSignal = Signal(“Text")
let label = UILabel()
label.bound(from: strSignal)
strSignal.next("New Label") //label reflects the
change on UI automatically
29
Signals
Functional
(convertible)
let one = Signal(0)
let two = Signal(0)
one.map { $0 + 1 }.bind(to: two)
one.next(1) // two = 2
30
Signals
Functional
(combinable into tuples):
let one = Signal(0), two = Signal(0)
let three = Signal(0), result = Signal(0)
one.zip(two).zip(three)
.map { $0 + $1 * $2 }.bind(to: result)
31
Signals
Functional
(filterable):
let one = Signal(0)
let two = Signal(0)
one.filter{ $0 > 5}.bind(to: two)
one.next(0) // two still 0
one.next(6) // two = 6
32
Signals
Functional
(flatMap):
Nobody knows how it should work.
33
Signals
Asynchronous
(context oriented)
let one = Signal(0)
one.on(ExecutionContext.main)
.observe{ print ($0) }
ExecutionContext.background.async{
one.next(1) // Prints 1 on main
}
34
Signals
Asynchronous
(debouncible)
let one = Signal(0)
one.debounce(0.1)
.observe{ print ("observe2", $0) }
one = 10
one = 20 // we see 20 only!!
35
Signals
Asynchronous
let one = Signal(0)
one.observe { print ("observer 1", $0) }
one.observe { print ("observer 2", $0) }
one.next(10) // guess order??? Who knows!!!
36
Promise/Futures
observable
let promise = Promise<Int>()
let future = promise.future
future.onSuccess { print($0) }
promise.success(10) // Prints 10
37
Promise/Futures
observable
let promise = Promise<Int>()
let future = promise.future
future.onFailure { print($0) }
promise.failure(NSError()) // Prints Error
38
Promise/Futures
Functional
(convertible, filterable)
let future = Future(value: 10)
future.filter { $0 > 0}
.map { "($0)" }
.onSuccess{ print($0) } // Prints “10”
39
Promise/Futures
Functional
(combinable into tuples):
let future1 = Future(value: 10)
let future2 = Future(value: 20)
future1.zip(future2)
.map { $0 + $1 }
.onSuccess{ print($0) } // Prints 30
40
Promise/Futures
Functional
(chains the order):
func loadUser()->Future<User>
func loadUserDetails(user: User) -> Future<UserDetails>
loadUser()
.flatMap { loadUserDetails($0) }
.onSuccess{ print ($0) } //Prints user details!
41
Futures
Asynchronous
(context oriented)
let promise = Promise<Int>()
let future = promise.future
future.on(ExecutionContext.main)
.onSuccess{ print ($0) }
ExecutionContext.background.async(in: .sec(2)) {
promise.success(10) // Will print 10 in 2 sec
}
42
State interpretation
43
Linear flows
44
let one = Signal()
let two = Signal()
let tree = Signal()
three.map { f2 }.bind(to: two)
two.map{ f1 }.bind(to: one)
Linear flows
45
var two {
didSet {
one = f1(newValue)
}
}
var three {
didSet {
two = f2(newValue)
}
}
One to many
46
let one = Signal()
let two = Signal()
let tree = Signal()
one.map { f1 }.bind(to: two)
one.map { f2 }.bind(to: tree)
One to many
47
var one {
didSet {
two = f1(newValue)
tree = f2(newValue)
}
}
Many to one
48
let one = Signal()
let two = Signal()
let tree = Signal()
one.zip(two).map { f($0, $1) }.bind(to: three)
Many to one
49
var one {
didSet {
three = f(newValue, two)
}
}
var two {
didSet {
three = f(one, newValue)
}
}
Order interpretation
50
Order
51
func loadUser() -> User
func loadUserPosts(user: User) -> [Post]
func loadUserComments(user: User) -> [Comment]
func loadUserDetails(user: User) -> UserDetails
func printData(posts: [Post], comments: [Comment])
func login() {
let user = loadUser()
let userDetails = loadUserDetails(user: user)
let posts = loadUserPosts(user: user)
let comments = loadUserComments(user: user)
printData(posts: posts, comments: comments)
}
Order
52
func loadUser() -> User
func loadUserPosts(user: User) -> [Post]
func loadUserComments(user: User) -> [Comment]
func loadUserDetails(user: User) -> UserDetails
func printData(posts: [Post], comments: [Comment])
func login() {
let user = loadUser()
let userDetails = loadUserDetails(user: user)
let posts = loadUserPosts(user: user)
let comments = loadUserComments(user: user)
printData(posts: posts, comments: comments)
}
Order
53
func loadUser() -> User
func loadUserPosts(user: User) -> [Post]
func loadUserComments(user: User) -> [Comment]
func printData(posts: [Post], comments: [Comment])
func loadUserDetails(user: User) -> UserDetails
func login() {
let user = loadUser()
let userDetails = loadUserDetails(user: user)
let posts = loadUserPosts(user: user)
let comments = loadUserComments(user: user)
printData(posts: posts, comments: comments)
}
Order
54
func loadUser() -> User
func loadUserPosts(user: User) -> [Post]
func loadUserComments(user: User) -> [Comment]
func printData(posts: [Post], comments: [Comment])
func loadUserDetails(user: User) -> UserDetails
func login() {
let user = loadUser()
let userDetails = loadUserDetails(user: user)
let posts = loadUserPosts(user: user)
let comments = loadUserComments(user: user)
printData(posts: posts, comments: comments)
}
Order
55
func loadUser() -> User
func loadUserPosts(user: User) -> [Post]
func loadUserComments(user: User) -> [Comment]
func printData(posts: [Post], comments: [Comment])
func loadUserDetails(user: User) -> UserDetails
func login() {
let user = loadUser()
let userDetails = loadUserDetails(user: user)
let posts = loadUserPosts(user: user)
let comments = loadUserComments(user: user)
printData(posts: posts, comments: comments)
}
Order
56
func loadUser() -> User
func loadUserPosts(user: User) -> [Post]
func loadUserComments(user: User) -> [Comment]
func printData(posts: [Post], comments: [Comment])
func loadUserDetails(user: User) -> UserDetails
func login() {
let user = loadUser()
let userDetails = loadUserDetails(user: user)
let comments = loadUserComments(user: user)
let posts = loadUserPosts(user: user)
printData(posts: posts, comments: comments)
}
Order
57
func loadUser() -> User
func loadUserPosts(user: User) -> [Post]
func loadUserComments(user: User) -> [Comment]
func loadUserDetails(user: User) -> UserDetails
func printData(posts: [Post], comments: [Comment])
func login() {
let user = loadUser()
let userDetails = loadUserDetails(user: user)
let posts = loadUserPosts(user: user)
let comments = loadUserComments(user: user)
printData(posts: posts, comments: comments)
}
Order
58
Return
Order
59
Return
func loadUser() -> Future<User>
func loadUserPosts(user: User) -> Future<[Post]>
func loadUserComments(user: User) -> Future<[Comment]>
func loadUserDetails(user: User) -> Future<UserDetails>
func printData(posts: [Post], comments: [Comment]) -> Future<Void>
Order
60
Return
func login() -> Future<Void> {
let user = loadUser()
let printData = user
.flatMap{ user in loadUserComments(user)
.zip{ loadUserComments(user) } }
.flatMap{ printData($0.0, $0.1) }
...
Order
61
Return
...
let userDetails = user.flatMap{ loadUserDetails($0) }
...
Order
62
Return
...
let printData = ... //Future<Void>
...
return printData.zip(userDetails).map {_ } // map to Future<Void>
}
Examples
63
Example
64
Name:
Password:
Login
Example
65
Name:
Password:
Login
Example
66
Name:
Password:
Login
Error:
Wrong login or password
OK
Example
class UserService {
func login(userName: String, password: String)-> Future<User>
}
...
class AppActions {
let showMainView = Signal<Void>()
let showError = Signal<Error>()
}
class LoginVM {
let error = Signal<Error>() // Handles error
let login = Signal<Void>() // Bound to UI, Handles login action
let name = Signal<String>() // Bound to UI
let password = Signal<String>() // Bound to UI
let busy = Signal<Bool>() // Bound to UI and shows/hides loader
...
Example
...
init(userService: UserService, actions: AppActions) {
//Busines logic
login // = Signal<Void>()
.with(weak: busy) // pass busy as weak pointer Signal
.with(value: name).with(value: password) //pass name, password as values
...
Example
...
.flatMap {
$0.0.bind(to: userService.login($0.1, $0.2)) // connect everything to real login
operation
}
...
Example
...
.feedError(to: error) // Guess what we have here
...
...
error.bind(to: actions.showError)
...
Example
...
.map {_ } // drop user to Signal<Void>
.bind(to: actions.showMainView) // pass signal to
...
Example
init(userService: UserService, actions: AppActions) {
//Busines logic
login // = Signal<Void>()
.with(weak: busy) // pass busy as weak pointer Signal
.with(value: name).with(value: password) //pass name, password as values
.flatMap{
$0.0.bind(to: userService.login($0.1, $0.2)) // connect everything to future
}
.feedError(to: error) // Guess what we have here
.map {_ } // drop user to Signal<Void>
.bind(to: actions.showMainView) // pass signal to
error.bind(to: actions.showError)
}
Example
class LoginController {
let login: UIButton!
let name: UITextField!
let password: UITextField!
func advise(vm: LoginVM) {
vm.name.bound(to: name)
vm.password.bound(to: password)
vm.login.bound(to: login)
vm.bind.bound(view.reactive.isActive)
}
}
Example
Conclusion
75
Why go FRP
from tech lead of mobile-team perspective:
If you need client application to be:
• Responsive
• Resilient
• Elastic
• Message Driven
FRP is for you!
76
Why go FRP
In additional you get:
• Clear code
• Avoid multithreading hell
• Reaction to changes using bindings.
• It’s easier to separate logic from UI. Controllers are thiner. MVVM!
77
Thanks
78

More Related Content

PDF
Python Asíncrono - Async Python
PPTX
#5 (Remote Method Invocation)
PDF
Kotlin coroutine - the next step for RxJava developer?
PDF
The Ring programming language version 1.3 book - Part 17 of 88
PPTX
Grand Central Dispatch in Objective-C
PDF
Beyond Breakpoints: Advanced Debugging with XCode
PDF
The Ring programming language version 1.7 book - Part 7 of 196
PDF
The Ring programming language version 1.5.1 book - Part 24 of 180
Python Asíncrono - Async Python
#5 (Remote Method Invocation)
Kotlin coroutine - the next step for RxJava developer?
The Ring programming language version 1.3 book - Part 17 of 88
Grand Central Dispatch in Objective-C
Beyond Breakpoints: Advanced Debugging with XCode
The Ring programming language version 1.7 book - Part 7 of 196
The Ring programming language version 1.5.1 book - Part 24 of 180

What's hot (20)

PDF
The Ring programming language version 1.5.3 book - Part 25 of 184
PPTX
Django - sql alchemy - jquery
PDF
Deferred
KEY
Into Clojure
KEY
Clojure Intro
PDF
The Ring programming language version 1.5.2 book - Part 25 of 181
PDF
isd312-05-wordnet
PDF
TDC2018SP | Trilha Kotlin - Programacao assincrona utilizando Coroutines
PDF
The Ring programming language version 1.7 book - Part 29 of 196
PPTX
Sequelize
PPTX
Entity Framework Core: tips and tricks
PDF
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
PPT
JDBC Core Concept
PDF
The Ring programming language version 1.5.2 book - Part 45 of 181
PDF
Creating Ext JS Extensions and Components
PDF
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
PDF
Clojure: The Art of Abstraction
PDF
Sam wd programs
PDF
Non stop random2b
PDF
Aplikasi rawat-inap-vbnet
The Ring programming language version 1.5.3 book - Part 25 of 184
Django - sql alchemy - jquery
Deferred
Into Clojure
Clojure Intro
The Ring programming language version 1.5.2 book - Part 25 of 181
isd312-05-wordnet
TDC2018SP | Trilha Kotlin - Programacao assincrona utilizando Coroutines
The Ring programming language version 1.7 book - Part 29 of 196
Sequelize
Entity Framework Core: tips and tricks
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
JDBC Core Concept
The Ring programming language version 1.5.2 book - Part 45 of 181
Creating Ext JS Extensions and Components
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
Clojure: The Art of Abstraction
Sam wd programs
Non stop random2b
Aplikasi rawat-inap-vbnet
Ad

Similar to FRP: What does "declarative" mean (20)

PDF
Functional programming in Swift
PDF
Intro to Reactive Programming with Swift
PDF
An introduction to functional programming with Swift
PPTX
RxJS ‘Marble’ programming
PDF
TDC2018SP | Trilha Prog Funcional - Programacao funcional na pratica com Swift
PDF
Reactive programming with RxSwift
PDF
ReactiveCocoa workshop
PDF
Swift rocks! #1
PDF
Tech fest
PDF
Introduction to Swift 2
PDF
Introduction To Functional Reactive Programming Poznan
PDF
What Swift can teach us all
PDF
Deep Dive Into Swift
PDF
Fun with functions
PDF
Programming Paradigms Which One Is The Best?
PDF
Isomorphic Reactive Programming
PDF
스위프트를 여행하는 히치하이커를 위한 스타일 안내
PDF
Reactive Programming with RxSwift
PPTX
Rx for Android & iOS by Harin Trivedi
PDF
Functionnal view modelling
Functional programming in Swift
Intro to Reactive Programming with Swift
An introduction to functional programming with Swift
RxJS ‘Marble’ programming
TDC2018SP | Trilha Prog Funcional - Programacao funcional na pratica com Swift
Reactive programming with RxSwift
ReactiveCocoa workshop
Swift rocks! #1
Tech fest
Introduction to Swift 2
Introduction To Functional Reactive Programming Poznan
What Swift can teach us all
Deep Dive Into Swift
Fun with functions
Programming Paradigms Which One Is The Best?
Isomorphic Reactive Programming
스위프트를 여행하는 히치하이커를 위한 스타일 안내
Reactive Programming with RxSwift
Rx for Android & iOS by Harin Trivedi
Functionnal view modelling
Ad

Recently uploaded (20)

PDF
6.-propertise of noble gases, uses and isolation in noble gases
PPTX
Phylogeny and disease transmission of Dipteran Fly (ppt).pptx
PPTX
Sustainable Forest Management ..SFM.pptx
PDF
natwest.pdf company description and business model
PDF
PM Narendra Modi's speech from Red Fort on 79th Independence Day.pdf
PPT
First Aid Training Presentation Slides.ppt
PPTX
power point presentation ofDracena species.pptx
PPTX
PurpoaiveCommunication for students 02.pptx
PDF
Module 7 guard mounting of security pers
PPTX
Phylogeny and disease transmission of Dipteran Fly (ppt).pptx
PDF
Unnecessary information is required for the
PPTX
Shizophrnia ppt for clinical psychology students of AS
PDF
Tunisia's Founding Father(s) Pitch-Deck 2022.pdf
PPTX
Anesthesia and it's stage with mnemonic and images
PDF
COLEAD A2F approach and Theory of Change
PPTX
CAPE CARIBBEAN STUDIES- Integration-1.pptx
PPTX
Lesson-7-Gas. -Exchange_074636.pptx
PDF
_Nature and dynamics of communities and community development .pdf
PPTX
Research Process - Research Methods course
PDF
MODULE 3 BASIC SECURITY DUTIES AND ROLES.pdf
6.-propertise of noble gases, uses and isolation in noble gases
Phylogeny and disease transmission of Dipteran Fly (ppt).pptx
Sustainable Forest Management ..SFM.pptx
natwest.pdf company description and business model
PM Narendra Modi's speech from Red Fort on 79th Independence Day.pdf
First Aid Training Presentation Slides.ppt
power point presentation ofDracena species.pptx
PurpoaiveCommunication for students 02.pptx
Module 7 guard mounting of security pers
Phylogeny and disease transmission of Dipteran Fly (ppt).pptx
Unnecessary information is required for the
Shizophrnia ppt for clinical psychology students of AS
Tunisia's Founding Father(s) Pitch-Deck 2022.pdf
Anesthesia and it's stage with mnemonic and images
COLEAD A2F approach and Theory of Change
CAPE CARIBBEAN STUDIES- Integration-1.pptx
Lesson-7-Gas. -Exchange_074636.pptx
_Nature and dynamics of communities and community development .pdf
Research Process - Research Methods course
MODULE 3 BASIC SECURITY DUTIES AND ROLES.pdf

FRP: What does "declarative" mean