Code europe

Introduction to
Functional Reactive
Programming
@EliSawic
About me
Eliasz Sawicki
Blog: www.eliaszsawicki.com
Twitter: @EliSawic
@EliSawic
Agenda
• What is functional reactive programming?
• Working with streams
• ReactiveSwift - Thinking in signals
• Example
@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
Functional
Programming
@EliSawic
Blocks
@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
Working with streams
@EliSawic
Stream
@EliSawic
Manipulating streams
@EliSawic
Map
@EliSawic
Filter
@EliSawic
Aggregating
@EliSawic
Manipulating
multiple streams
@EliSawic
Combine latest
@EliSawic
Merge
@EliSawic
Chaining streams
@EliSawic
www.rxmarbles.com
@EliSawic
ReactiveSwift
@EliSawic
Thinking in Signals
@EliSawic
What is a signal?
@EliSawic
This screen is a signal
@EliSawic
Represents events
over time
@EliSawic
Observing does not
trigger side effects
@EliSawic
No random access to
events
@EliSawic
Observe
@EliSawic
If you don't listen, it's
gone
@EliSawic
What is event?
@EliSawic
Event
struct Idea {
var content: String
var quality: Quality
}
enum Quality {
case Great
case Average
case Worst
}
@EliSawic
Non-Terminating
• Next
@EliSawic
Terminating
• Completed
• Failed
• Interrupted (Reactive Cocoa)
@EliSawic
Presentation
let (presentation, presentationObserver) = Signal<Idea, NoError>.pipe()
let content = "This presentation is a signal"
let idea = Idea(content: content, quality: .Great)
presentationObserver.send(value: idea)
presentationObserver.sendCompleted()
@EliSawic
Observing
presentation.observeValues { idea in
remember(idea: idea)
}
presentation.observeCompleted {
print("Time for a break")
}
@EliSawic
Only great ideas
let greatIdeas = presentation.filter { idea in
idea.quality == .Great
}
greatIdeas.observeValues { greatIdea in
remember(idea: greatIdea)
}
@EliSawic
Positive listener
let greatPresentation = presentation.map { idea -> Idea in
var greatIdea = idea
greatIdea.quality = .Great
return greatIdea
}
@EliSawic
Signal producer
@EliSawic
Represents a tasks
@EliSawic
Possible side effects
@EliSawic
Does not start it's
work if not asked
@EliSawic
Run presentation
func runPresentation() -> SignalProducer<Idea, NoError> {
return SignalProducer { observer, _ in
observer.send(value: idea1)
observer.send(value: idea2)
...
observer.sendCompleted()
}
}
@EliSawic
Work with presentation
runPresentation().startWithValues { idea in
print(idea)
}
runPresentation().startWithSignal { (signal, _) in
signal.observeValues({ idea in
print(idea)
})
signal.observeCompleted {
print("Finally...")
}
}
@EliSawic
Cold vs Hot
@EliSawic
Properties
@EliSawic
Mutable Property
let firstSlide = Slide(number: 1)
let slide = MutableProperty<Slide>(firstSlide)
slide.producer.startWithValues { (text) in
print(text)
}
slide.value = Slide(number: 2)
@EliSawic
Bindings
@EliSawic
Binding example
let slideNumber = MutableProperty<Int>(0)
let (signal, _) = Signal<Slide, NoError>.pipe()
slideNumber <~ signal.map { return $0.number }
@EliSawic
Binding example
let (signal, _) = Signal<Slide, NoError>.pipe()
label.reactive.text <~ signal.map { return "Slide: ($0.number)" }
@EliSawic
Schedulers
@EliSawic
Know where you are
signal.observe(on: QueueScheduler.main).observeValues { idea in
print("Performing UI updates")
}
@EliSawic
Memory Management
@EliSawic
Disposables
@EliSawic
Free your memory
let disposable = signal.observeValues { value in
...
}
disposable.dispose()
@EliSawic
Release your producers
let disposable = producer.startWithValues { value in
...
}
disposable.dispose()
@EliSawic
Example
@EliSawic
@EliSawic
How does it work?
@EliSawic
Is name valid?
let isValidNameSignal = nameSignal.map { (name) -> Bool in
return name.characters.count > 2
}
@EliSawic
Is surname valid?
let isValidSurnameSignal = surnameSignal.map { (surname) -> Bool in
return surname.characters.count > 2
}
@EliSawic
Is mail valid?
let isValidMailSignal = 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 formDataSignal = combineLatest(
isValidNameSignal,
isValidSurnameSignal,
isValidMailSignal)
@EliSawic
Is form valid?
let isValidFormSignal = formDataSignal.map {
(isValidName, isValidSurname, isValidMail) -> Bool in
return isValidMail && isValidSurname && isValidMail
}
@EliSawic
Update the state
isValidFormSignal.observeValues { isValid in
updateButtonWith(state: isValid)
}
@EliSawic
Binding
let isValidForm = MutableProperty<Bool>(false)
isValidForm <~ isFormValidSignal
@EliSawic
Conclusion
@EliSawic
Thank you for your
attention!
sendCompleted()
@EliSawic
1 of 71

Recommended

Introduction To Functional Reactive Programming Poznan by
Introduction To Functional Reactive Programming PoznanIntroduction To Functional Reactive Programming Poznan
Introduction To Functional Reactive Programming PoznanEliasz Sawicki
802 views82 slides
201707 CSE110 Lecture 25 by
201707 CSE110 Lecture 25   201707 CSE110 Lecture 25
201707 CSE110 Lecture 25 Javier Gonzalez-Sanchez
113 views14 slides
201707 CSE110 Lecture 24 by
201707 CSE110 Lecture 24   201707 CSE110 Lecture 24
201707 CSE110 Lecture 24 Javier Gonzalez-Sanchez
39 views18 slides
R vectors by
R   vectorsR   vectors
R vectorsLearnbay Datascience
48 views12 slides
Introduction to Functional Reactive Programming by
Introduction to Functional Reactive ProgrammingIntroduction to Functional Reactive Programming
Introduction to Functional Reactive ProgrammingEliasz Sawicki
343 views79 slides
8 Strategies for the Ideal Apprenticeship by
8 Strategies for the Ideal Apprenticeship8 Strategies for the Ideal Apprenticeship
8 Strategies for the Ideal ApprenticeshipRobert Greene
216.1K views12 slides

More Related Content

Similar to Code europe

The Evolution of Async-Programming (SD 2.0, JavaScript) by
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
2K views64 slides
Crossing the Bridge: Connecting Rails and your Front-end Framework by
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkDaniel Spector
1.3K views65 slides
Reactive programming with RxJS - Taiwan by
Reactive programming with RxJS - TaiwanReactive programming with RxJS - Taiwan
Reactive programming with RxJS - Taiwanmodernweb
231 views107 slides
The Evolution of Async-Programming on .NET Platform (.Net China, C#) by
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
2.1K views61 slides
Reactive Applications in Enterprise Java by
Reactive Applications in Enterprise JavaReactive Applications in Enterprise Java
Reactive Applications in Enterprise JavaOPEN KNOWLEDGE GmbH
337 views59 slides
Architecture for scalable Angular applications by
Architecture for scalable Angular applicationsArchitecture for scalable Angular applications
Architecture for scalable Angular applicationsPaweł Żurowski
729 views65 slides

Similar to Code europe(20)

The Evolution of Async-Programming (SD 2.0, JavaScript) by jeffz
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)
jeffz2K views
Crossing the Bridge: Connecting Rails and your Front-end Framework by Daniel Spector
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end Framework
Daniel Spector1.3K views
Reactive programming with RxJS - Taiwan by modernweb
Reactive programming with RxJS - TaiwanReactive programming with RxJS - Taiwan
Reactive programming with RxJS - Taiwan
modernweb231 views
The Evolution of Async-Programming on .NET Platform (.Net China, C#) by 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#)
jeffz2.1K views
Architecture for scalable Angular applications by Paweł Żurowski
Architecture for scalable Angular applicationsArchitecture for scalable Angular applications
Architecture for scalable Angular applications
Paweł Żurowski729 views
Javaslang Talk @ Javaland 2017 by David Schmitz
Javaslang Talk @ Javaland 2017Javaslang Talk @ Javaland 2017
Javaslang Talk @ Javaland 2017
David Schmitz780 views
Programming Sideways: Asynchronous Techniques for Android by Emanuele Di Saverio
Programming Sideways: Asynchronous Techniques for AndroidProgramming Sideways: Asynchronous Techniques for Android
Programming Sideways: Asynchronous Techniques for Android
Emanuele Di Saverio1.6K views
Introduction to Reactive Extensions without saying functional reactive by Tetsuharu OHZEKI
Introduction to Reactive Extensions without saying functional reactiveIntroduction to Reactive Extensions without saying functional reactive
Introduction to Reactive Extensions without saying functional reactive
Tetsuharu OHZEKI611 views
Charlie Gerard- Mind Control in Javascript (Evolution) by Thoughtworks
Charlie Gerard- Mind Control in Javascript (Evolution)Charlie Gerard- Mind Control in Javascript (Evolution)
Charlie Gerard- Mind Control in Javascript (Evolution)
Thoughtworks836 views
Functional Swift by Geison Goes
Functional SwiftFunctional Swift
Functional Swift
Geison Goes232 views
Functional Programming With Scala by Knoldus Inc.
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
Knoldus Inc.802 views
OSMC 2023 | Built-in OpenTelemetry support in Elasticsearch clients by Greg K... by NETWAYS
OSMC 2023 | Built-in OpenTelemetry support in Elasticsearch clients by Greg K...OSMC 2023 | Built-in OpenTelemetry support in Elasticsearch clients by Greg K...
OSMC 2023 | Built-in OpenTelemetry support in Elasticsearch clients by Greg K...
NETWAYS12 views

More from Eliasz Sawicki

Redux - 4Developers by
Redux - 4DevelopersRedux - 4Developers
Redux - 4DevelopersEliasz Sawicki
130 views110 slides
Eliasz sawickimeetupit by
Eliasz sawickimeetupitEliasz sawickimeetupit
Eliasz sawickimeetupitEliasz Sawicki
110 views20 slides
Developing more in less time by
Developing more in less timeDeveloping more in less time
Developing more in less timeEliasz Sawicki
138 views121 slides
The art-of-developing-more-in-less-time-berlin by
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
273 views111 slides
Introduction to react native by
Introduction to react nativeIntroduction to react native
Introduction to react nativeEliasz Sawicki
481 views78 slides
Doing more in less time - Mobiconf by
Doing more in less time - MobiconfDoing more in less time - Mobiconf
Doing more in less time - MobiconfEliasz Sawicki
390 views107 slides

More from Eliasz Sawicki(10)

Recently uploaded

Microsoft Power Platform.pptx by
Microsoft Power Platform.pptxMicrosoft Power Platform.pptx
Microsoft Power Platform.pptxUni Systems S.M.S.A.
67 views38 slides
Data Integrity for Banking and Financial Services by
Data Integrity for Banking and Financial ServicesData Integrity for Banking and Financial Services
Data Integrity for Banking and Financial ServicesPrecisely
56 views26 slides
DRBD Deep Dive - Philipp Reisner - LINBIT by
DRBD Deep Dive - Philipp Reisner - LINBITDRBD Deep Dive - Philipp Reisner - LINBIT
DRBD Deep Dive - Philipp Reisner - LINBITShapeBlue
62 views21 slides
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue by
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueElevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueShapeBlue
96 views7 slides
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T by
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&TCloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&TShapeBlue
56 views34 slides
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue by
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlueWhat’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlueShapeBlue
131 views23 slides

Recently uploaded(20)

Data Integrity for Banking and Financial Services by Precisely
Data Integrity for Banking and Financial ServicesData Integrity for Banking and Financial Services
Data Integrity for Banking and Financial Services
Precisely56 views
DRBD Deep Dive - Philipp Reisner - LINBIT by ShapeBlue
DRBD Deep Dive - Philipp Reisner - LINBITDRBD Deep Dive - Philipp Reisner - LINBIT
DRBD Deep Dive - Philipp Reisner - LINBIT
ShapeBlue62 views
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue by ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueElevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
ShapeBlue96 views
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T by ShapeBlue
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&TCloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
ShapeBlue56 views
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue by ShapeBlue
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlueWhat’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue
ShapeBlue131 views
State of the Union - Rohit Yadav - Apache CloudStack by ShapeBlue
State of the Union - Rohit Yadav - Apache CloudStackState of the Union - Rohit Yadav - Apache CloudStack
State of the Union - Rohit Yadav - Apache CloudStack
ShapeBlue145 views
Backroll, News and Demo - Pierre Charton, Matthias Dhellin, Ousmane Diarra - ... by ShapeBlue
Backroll, News and Demo - Pierre Charton, Matthias Dhellin, Ousmane Diarra - ...Backroll, News and Demo - Pierre Charton, Matthias Dhellin, Ousmane Diarra - ...
Backroll, News and Demo - Pierre Charton, Matthias Dhellin, Ousmane Diarra - ...
ShapeBlue83 views
Business Analyst Series 2023 - Week 4 Session 7 by DianaGray10
Business Analyst Series 2023 -  Week 4 Session 7Business Analyst Series 2023 -  Week 4 Session 7
Business Analyst Series 2023 - Week 4 Session 7
DianaGray1080 views
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De... by Moses Kemibaro
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...
Moses Kemibaro29 views
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue by ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlueCloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
ShapeBlue46 views
Five Things You SHOULD Know About Postman by Postman
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About Postman
Postman40 views
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda... by ShapeBlue
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...
Hypervisor Agnostic DRS in CloudStack - Brief overview & demo - Vishesh Jinda...
ShapeBlue63 views
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava... by ShapeBlue
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...
ShapeBlue48 views
Why and How CloudStack at weSystems - Stephan Bienek - weSystems by ShapeBlue
Why and How CloudStack at weSystems - Stephan Bienek - weSystemsWhy and How CloudStack at weSystems - Stephan Bienek - weSystems
Why and How CloudStack at weSystems - Stephan Bienek - weSystems
ShapeBlue111 views
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue by ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlueCloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
ShapeBlue46 views
"Surviving highload with Node.js", Andrii Shumada by Fwdays
"Surviving highload with Node.js", Andrii Shumada "Surviving highload with Node.js", Andrii Shumada
"Surviving highload with Node.js", Andrii Shumada
Fwdays40 views
HTTP headers that make your website go faster - devs.gent November 2023 by Thijs Feryn
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023
Thijs Feryn28 views

Code europe