SlideShare uma empresa Scribd logo
1 de 79
Baixar para ler offline
About me
Eliasz Sawicki
Blog: www.eliaszsawicki.com
Twitter: @EliSawic
@EliSawic
Introduction to
Functional Reactive
Programming
@EliSawic
Agenda
• What is Functional Reactive Programming?
• Let's take a look at ReactiveCocoa
• Working with streams
• Real life example
• Conclusion
@EliSawic
Functional Reactive
Programming
@EliSawic
Wikipedia
Functional reactive programming (FRP) is a programming
paradigm for reactive programming (asynchronous dataflow
programming) using the building blocks of functional
programming (e.g. map, reduce, filter).
@EliSawic
Reactive
Programming
@EliSawic
Asynchronous
Dataflow
@EliSawic
Reacting to state
changes
@EliSawic
Functional
Programming
@EliSawic
Immutable
@EliSawic
assert(f(x) == f(x))
@EliSawic
A person
class Person {
let name: String
let phoneNumber: String
init(name: String, phoneNumber: String) {
self.name = name
self.phoneNumber = phoneNumber
}
}
class MobilePhone {
func call(person: Person) -> Bool {
// implementation
}
}
@EliSawic
Mutable
let mobilePhone = MobilePhone()
let john = Person(name: "John", phoneNumber: "123456789")
func makeAPhoneCall(device: Phone, person: Person, countryCode: String) -> Bool {
person.phoneNumber = countryCode + person.phoneNumber
let success = device.call(person)
return success
}
makeAPhoneCall(device: mobilePhone, person: john, countryCode: "+48") // true
makeAPhoneCall(device: mobilePhone, person: john, countryCode: "+48") // false
makeAPhoneCall(device: mobilePhone, person: john, countryCode: "+48") // false
@EliSawic
Immutable
let mobilePhone = MobilePhone()
let john = Person(name: "John", phoneNumber: "123456789")
func makeAPhoneCall(device: Phone, person: Person, countryCode: String) -> Bool {
let prefixedPhoneNumber = countryCode + person.phoneNumber
let newPerson = Person(name: person.name, phoneNumber: prefixedPhoneNumber)
let success = device.call(newPerson)
return success
}
makeAPhoneCall(device: mobilePhone, person: john, countryCode: "+48") // true
makeAPhoneCall(device: mobilePhone, person: john, countryCode: "+48") // true
makeAPhoneCall(device: mobilePhone, person: john, countryCode: "+48") // true
@EliSawic
Stateless
@EliSawic
Stateful
var value = 0
func increment() -> Int {
value += 1
return value
}
@EliSawic
Stateless
func increment(value: Int) -> Int {
return value + 1
}
@EliSawic
Functional Reactive
Programming
@EliSawic
Imperative
vs
Declarative
@EliSawic
Imperative
@EliSawic
Imperative
let array = [0, 1, 2, 3, 4, 5]
var evenNumbers = [Int]()
for element in array {
if element % 2 == 0 {
evenNumbers.append(element)
}
}
@EliSawic
Declarative
@EliSawic
Declarative
let array = [0, 1, 2, 3, 4, 5]
let evenNumbers = array.filter { $0 % 2 == 0 }
@EliSawic
ReactiveCocoa
@EliSawic
Event streams
@EliSawic
Event Stream
@EliSawic
Event
@EliSawic
Non-Terminating
• Next
@EliSawic
Terminating
• Completed
• Failed
• Interrupted (Reactive Cocoa)
@EliSawic
Signal
@EliSawic
Events over time
@EliSawic
No side effects
@EliSawic
No random access to
events
@EliSawic
Must be observed in
order to access it's
events
@EliSawic
Hot
@EliSawic
Signal's lifetime
• Passes any number of Next events
• "Dies" when terminating event
• Any new observer will receive Interrupted event
@EliSawic
Observing
let someWork = Signal<Int, NoError> { observer in
observer.sendNext(1)
observer.sendNext(2)
....
observer.sendNext(1000000)
observer.sendCompleted()
}
signal.observe { (event) in
print(event)
}
signal.observeNext { (value) in
print(value)
}
signal.observeCompleted {
print("Completed")
}
@EliSawic
Pipe
@EliSawic
Pipe
let (signal, observer) = Signal<String, NoError>.pipe()
signal.observeNext({ text in
print(text)
})
signal.observeCompleted({
print("Test completed")
})
observer.sendNext("It's a test") // It's a test
observer.sendCompleted() // Test completed
@EliSawic
SignalProducer
@EliSawic
Represents tasks
@EliSawic
Creates signal
@EliSawic
Possible side effects
@EliSawic
Does not start it's
work if not started
@EliSawic
Cold
@EliSawic
Using Signal Producer
let producer = SignalProducer<String, NSError> { (observer, composite) in
observer.sendNext("In Progress...")
// ......
observer.sendCompleted()
}
producer.startWithSignal { (signal, _) in
signal.observeNext({ (text) in
print(text) // In Progress...
})
signal.observeCompleted({
print("Test completed") // Test completed
})
}
@EliSawic
Cold vs Hot
@EliSawic
Properties
@EliSawic
MutableProperty
let name = MutableProperty("Bob")
name.producer.startWithNext { (text) in
print(text)
}
name.value = "Lisa"
@EliSawic
Bindings
@EliSawic
Basic binding
let property = MutableProperty<String>("")
let (signal, _) = Signal<String, NoError>.pipe()
property <~ signal
@EliSawic
Schedulers
@EliSawic
Memory Management
@EliSawic
Disposables
@EliSawic
Manipulating signals
@EliSawic
Map
@EliSawic
Filter
@EliSawic
Aggregating
@EliSawic
Skip repeats
@EliSawic
Manipulating
multiple signals
@EliSawic
Combine latest
@EliSawic
Zip
@EliSawic
Merge
@EliSawic
Chaining operators
@EliSawic
Chain them all!
let newSignalX = signalX.skipRepeats()
.filter { x > 2 }
.map { x * 10 }
let newSignalY = signalY.filter { x > 10 }
let combined = combineLatest(newSignalX, newSignalY)
combined.observeNext { xValue, yValue in
print("Update: (xValue) : (yValue)")
}
@EliSawic
Example
@EliSawic
@EliSawic
@EliSawic
@EliSawic
@EliSawic
@EliSawic
How does it work?
@EliSawic
Is name valid?
let isValidName = nameSignal.map { (name) -> Bool in
return input.characters.count > 2
}
@EliSawic
Is mail valid?
let isValidMail = mailSignal.map { (mail) -> Bool in
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,}"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailTest.evaluateWithObject(mail)
}
@EliSawic
Combine Latest
let formData = combineLatest(isValidName,
isValidSurname,
isValidMail)
@EliSawic
Is form valid?
disposables += isFormValid <~ formData.map {
(isValidName, isValidSurname, isValidMail) -> Bool in
return isValidMail && isValidSurname && isValidMail
}
@EliSawic
Button state
let producer = isFormValid.producer.skipRepeats()
disposables += producer.startWithNext {[unowned self] (isValid) in
self.updateAcceptButtonWithState(isValid)
}
@EliSawic
Conclusion
@EliSawic
Thank you for your
attention!
@EliSawic

Mais conteúdo relacionado

Semelhante a Introduction to Functional Reactive Programming

The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)jeffz
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)jeffz
 
Functional Reactive Endpoints using Spring 5
Functional Reactive Endpoints using Spring 5Functional Reactive Endpoints using Spring 5
Functional Reactive Endpoints using Spring 5Rory Preddy
 
Reactive Streams, j.u.concurrent, & Beyond!
Reactive Streams, j.u.concurrent, & Beyond!Reactive Streams, j.u.concurrent, & Beyond!
Reactive Streams, j.u.concurrent, & Beyond!C4Media
 
Building Self-Defending Applications With OWASP AppSensor JavaOne 2016
Building Self-Defending Applications With OWASP AppSensor JavaOne 2016Building Self-Defending Applications With OWASP AppSensor JavaOne 2016
Building Self-Defending Applications With OWASP AppSensor JavaOne 2016jtmelton
 
Intro to ReactiveCocoa
Intro to ReactiveCocoaIntro to ReactiveCocoa
Intro to ReactiveCocoakleneau
 
Akka.NET streams and reactive streams
Akka.NET streams and reactive streamsAkka.NET streams and reactive streams
Akka.NET streams and reactive streamsBartosz Sypytkowski
 
AppSensor CodeMash 2017
AppSensor CodeMash 2017AppSensor CodeMash 2017
AppSensor CodeMash 2017jtmelton
 
Reactive Extensions .NET
Reactive Extensions .NETReactive Extensions .NET
Reactive Extensions .NETGeorge Taskos
 
Alexey Golub - Dependency absolution (application as a pipeline) | Svitla Sma...
Alexey Golub - Dependency absolution (application as a pipeline) | Svitla Sma...Alexey Golub - Dependency absolution (application as a pipeline) | Svitla Sma...
Alexey Golub - Dependency absolution (application as a pipeline) | Svitla Sma...Oleksii Holub
 
Orleankka Intro Circa 2015
Orleankka Intro Circa 2015Orleankka Intro Circa 2015
Orleankka Intro Circa 2015Yevhen Bobrov
 
The Need for Async @ ScalaWorld
The Need for Async @ ScalaWorldThe Need for Async @ ScalaWorld
The Need for Async @ ScalaWorldKonrad Malawski
 
Reactive Applications in Enterprise Java
Reactive Applications in Enterprise JavaReactive Applications in Enterprise Java
Reactive Applications in Enterprise JavaOPEN KNOWLEDGE GmbH
 
End to End Akka Streams / Reactive Streams - from Business to Socket
End to End Akka Streams / Reactive Streams - from Business to SocketEnd to End Akka Streams / Reactive Streams - from Business to Socket
End to End Akka Streams / Reactive Streams - from Business to SocketKonrad Malawski
 
Need for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsNeed for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsKonrad Malawski
 
A Playful Introduction to Rx
A Playful Introduction to RxA Playful Introduction to Rx
A Playful Introduction to RxAndrey Cheptsov
 
Reactive programming
Reactive programmingReactive programming
Reactive programmingNick Hodge
 

Semelhante a Introduction to Functional Reactive Programming (20)

The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
 
Functional Reactive Endpoints using Spring 5
Functional Reactive Endpoints using Spring 5Functional Reactive Endpoints using Spring 5
Functional Reactive Endpoints using Spring 5
 
Reactive Streams, j.u.concurrent, & Beyond!
Reactive Streams, j.u.concurrent, & Beyond!Reactive Streams, j.u.concurrent, & Beyond!
Reactive Streams, j.u.concurrent, & Beyond!
 
Coscup
CoscupCoscup
Coscup
 
Building Self-Defending Applications With OWASP AppSensor JavaOne 2016
Building Self-Defending Applications With OWASP AppSensor JavaOne 2016Building Self-Defending Applications With OWASP AppSensor JavaOne 2016
Building Self-Defending Applications With OWASP AppSensor JavaOne 2016
 
Intro to ReactiveCocoa
Intro to ReactiveCocoaIntro to ReactiveCocoa
Intro to ReactiveCocoa
 
Akka.NET streams and reactive streams
Akka.NET streams and reactive streamsAkka.NET streams and reactive streams
Akka.NET streams and reactive streams
 
RxJava Applied
RxJava AppliedRxJava Applied
RxJava Applied
 
AppSensor CodeMash 2017
AppSensor CodeMash 2017AppSensor CodeMash 2017
AppSensor CodeMash 2017
 
Reactive Extensions .NET
Reactive Extensions .NETReactive Extensions .NET
Reactive Extensions .NET
 
Alexey Golub - Dependency absolution (application as a pipeline) | Svitla Sma...
Alexey Golub - Dependency absolution (application as a pipeline) | Svitla Sma...Alexey Golub - Dependency absolution (application as a pipeline) | Svitla Sma...
Alexey Golub - Dependency absolution (application as a pipeline) | Svitla Sma...
 
Orleankka Intro Circa 2015
Orleankka Intro Circa 2015Orleankka Intro Circa 2015
Orleankka Intro Circa 2015
 
The Need for Async @ ScalaWorld
The Need for Async @ ScalaWorldThe Need for Async @ ScalaWorld
The Need for Async @ ScalaWorld
 
Reactive Applications in Enterprise Java
Reactive Applications in Enterprise JavaReactive Applications in Enterprise Java
Reactive Applications in Enterprise Java
 
End to End Akka Streams / Reactive Streams - from Business to Socket
End to End Akka Streams / Reactive Streams - from Business to SocketEnd to End Akka Streams / Reactive Streams - from Business to Socket
End to End Akka Streams / Reactive Streams - from Business to Socket
 
Tdd iPhone For Dummies
Tdd iPhone For DummiesTdd iPhone For Dummies
Tdd iPhone For Dummies
 
Need for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsNeed for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applications
 
A Playful Introduction to Rx
A Playful Introduction to RxA Playful Introduction to Rx
A Playful Introduction to Rx
 
Reactive programming
Reactive programmingReactive programming
Reactive programming
 

Mais de Eliasz Sawicki

Eliasz sawickimeetupit
Eliasz sawickimeetupitEliasz sawickimeetupit
Eliasz sawickimeetupitEliasz Sawicki
 
Developing more in less time
Developing more in less timeDeveloping more in less time
Developing more in less timeEliasz Sawicki
 
The art-of-developing-more-in-less-time-berlin
The art-of-developing-more-in-less-time-berlinThe art-of-developing-more-in-less-time-berlin
The art-of-developing-more-in-less-time-berlinEliasz Sawicki
 
Introduction to react native
Introduction to react nativeIntroduction to react native
Introduction to react nativeEliasz Sawicki
 
Doing more in less time - Mobiconf
Doing more in less time - MobiconfDoing more in less time - Mobiconf
Doing more in less time - MobiconfEliasz Sawicki
 
Time traveling with ReSwift
Time traveling with ReSwiftTime traveling with ReSwift
Time traveling with ReSwiftEliasz Sawicki
 
ReSwift CocoaHeads Tricity
ReSwift CocoaHeads TricityReSwift CocoaHeads Tricity
ReSwift CocoaHeads TricityEliasz Sawicki
 
ReactiveCocoa workshop
ReactiveCocoa workshopReactiveCocoa workshop
ReactiveCocoa workshopEliasz Sawicki
 

Mais de Eliasz Sawicki (10)

Redux - 4Developers
Redux - 4DevelopersRedux - 4Developers
Redux - 4Developers
 
Eliasz sawickimeetupit
Eliasz sawickimeetupitEliasz sawickimeetupit
Eliasz sawickimeetupit
 
Developing more in less time
Developing more in less timeDeveloping more in less time
Developing more in less time
 
The art-of-developing-more-in-less-time-berlin
The art-of-developing-more-in-less-time-berlinThe art-of-developing-more-in-less-time-berlin
The art-of-developing-more-in-less-time-berlin
 
Introduction to react native
Introduction to react nativeIntroduction to react native
Introduction to react native
 
Doing more in less time - Mobiconf
Doing more in less time - MobiconfDoing more in less time - Mobiconf
Doing more in less time - Mobiconf
 
Time traveling with ReSwift
Time traveling with ReSwiftTime traveling with ReSwift
Time traveling with ReSwift
 
Calabash
CalabashCalabash
Calabash
 
ReSwift CocoaHeads Tricity
ReSwift CocoaHeads TricityReSwift CocoaHeads Tricity
ReSwift CocoaHeads Tricity
 
ReactiveCocoa workshop
ReactiveCocoa workshopReactiveCocoa workshop
ReactiveCocoa workshop
 

Último

Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPowerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPsychicRuben LoveSpells
 
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRFULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRnishacall1
 
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Servicenishacall1
 
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceDelhi Call girls
 
Leading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfLeading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfCWS Technology
 

Último (6)

Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
 
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPowerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
 
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRFULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
 
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
 
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
 
Leading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfLeading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdf
 

Introduction to Functional Reactive Programming