SlideShare a Scribd company logo
DEMYSTIFYING
REACTIVE
PROGRAMMING
Dr. Tom Bulatewicz – ExakTime, Inc.
Seattle CodeCamp – 9 September 2017
Blog article at medium.com/@tombcz
“Reactive Programming”
“WTF is Reactive Programming” by Evgeny Poberezkin
“Reactive” dates back to at least 1969
What is a
Reactive
System?
“accepts inputs, performs
transformations on them and
produces outputs”
“repeatedly prompted
by the outside world and
their role is to
continuously respond to
external inputs”
“On the development of reactive systems”, Harel and
Pnueli, 1985
vs.
Transformational
System
Reactive System Transformational
System
PNG
Image Converter
JPG
decode &
encode
temperature
Thermostat
on
< 65
off
Reactive
(Software)
Systems
Today refers to large software
systems as a whole
Parts of the system are decoupled
in space and time via message
passing
A reactive software system is a
group of reactive systems, each
responding to streams of
messages
Responsive
Message Driven
ResilientElastic
Value
Form
Means
www.reactivemanifesto.org
Reactive Program
The analog of message
passing in a system is
eventing in a program
Reactive System
Responsive
Event Driven
ResilientElastic
Value
Form
Means
Code
Code
Code
Code
Code
Code
streams of events
Modeling code components as reactive systems
Key Concept #1
Observation -
Decoupling
Code in Time
To model a piece of code
like a reactive system is to
give it the ability to respond
to other code
Some sort of observation –
like a callback, delegate,
event, etc.
Object A Object B
start observing
notify
notify
notify
stop observing
Observation
class A {
void processAll() {
while(b.hasItems()) {
process(b.next());
}
}
}
Class A
Class B
next() observe() notify()
class A {
public A() {
b.observe(this);
}
void notify(item) {
process(item);
}
}
Declarative vs.
Imperative
Reactive-style components
lend themselves to being
declarative
Say how things are, not how
to do them
5:00 Clock
Clock
5:00
5:00
“The time should be here”
Each second:
- Get the time
- Update the view
5:00
time?
Declarative Programming
1
2
3
4
A
5
4
= A1 + A2
B
Code Spreadsheet
var x = 5;
var y = 4;
var s <=> x + y; // s == 9
x = 9; // s == 13
do something
Reactive
Code
Other
Code
notify
Code reacts to streams of events
Key Concept #2
Benefits: Reduces dependencies & facilitates declarative code style
Where can we apply this?
User Interface Logic
Datastore
REST Client
Cloud
class ViewModel {
void update() {
var data = await restClient.Get();
dataStore.Save(data);
data = Logic(data);
UpdateUI(data);
}
}
User Interface
Datastore
REST Client
Cloud
ViewModel
Business
Logic
class ViewModel {
void notify(data) {
UpdateUI(data);
}
}
User Interface
Datastore
REST Client
Cloud
ViewModel
Business
Logic
Provider
notify()
ViewModel Provider
observe entity X
notify
notify
stop observing
update X
REST call and save
update X
REST call and save
View
Datastore
REST Client
Cloud
ViewModel
Business
Logic
Provider
notify()notify()
ViewModel Provider
observe entity X
notify
update X
View
bind property Y
notify
Anything (but not everything)
• Closely coupled
• Direct method calls
• Exchange mutable
objects
• Decoupled
• Observation
• Exchange immutable
objects
Reactive Extensions
■ Create observables and observe them
■ Manipulate the flow of events
Observer Observable
subscribe()
onNext()
Op
Example
var x = 5;
var y = 4;
var s <=> x + y; // s == 9
x = 9; // s == 13
var x = new Number { Value = 5 };
var y = new Number { Value = 4 };
var s = new Sum(x, y);
Console.WriteLine($"s={s.Value}");
x.Value = 9;
Console.WriteLine($"s={s.Value}");
https://goo.gl/gn3iTe
class Number
{
private int _value;
public int Value {
get { return _value; }
set { _value = value;
_subject.OnNext(_value); }
}
        private readonly Subject<int> _subject = new Subject<int>();
        public IObservable<int> Changed {
            get { return _subject.AsObservable(); }
        }
}
class Sum
{
private int _x;
private int _y;
private int _sum;
public int Value { get { return _sum; } }
        public Sum(Number x, Number y)
        {
_x = x.Value;
_y = y.Value;
            _sum = _x + _y;
            x.Changed.Subscribe((n) => { _x = n; _sum = _x + _y; });
            y.Changed.Subscribe((n) => { _y = n; _sum = _x + _y; });
        }
    }
RX Operators – Creation
Create an observable that emits a sequence of integers spaced by a
given time interval
Interval
Interval( x sec. )
x sec. x sec. x sec. x sec.
0 1 2 3
Suppress duplicate items emitted by an observable
Distinct
RX Operators – Filtering
1 2 2 1
1 2 3
3
Distinct
Transform the items emitted by an Observable by applying a function to
each of them
Map
RX Operators – Transformation
1 2 3
10 20 30
Map( x => 10 * x )
Combine multiple Observables into one by merging their emissions
Merge
RX Operators – Joining
20 40 60
Merge
1 1
80 100
20 40 60 80 1001 1
Summary
■ Reactive programming can be thought of as modeling code
as individual reactive systems
■ Decoupling code via observation reduces dependencies and
facilitates declarative programming
■ Reactive Extensions provide an implementation of the
Observer pattern along with operators
QUESTIONS?

More Related Content

Demystifying Reactive Programming

  • 1. DEMYSTIFYING REACTIVE PROGRAMMING Dr. Tom Bulatewicz – ExakTime, Inc. Seattle CodeCamp – 9 September 2017 Blog article at medium.com/@tombcz
  • 2. “Reactive Programming” “WTF is Reactive Programming” by Evgeny Poberezkin “Reactive” dates back to at least 1969
  • 3. What is a Reactive System? “accepts inputs, performs transformations on them and produces outputs” “repeatedly prompted by the outside world and their role is to continuously respond to external inputs” “On the development of reactive systems”, Harel and Pnueli, 1985 vs. Transformational System
  • 4. Reactive System Transformational System PNG Image Converter JPG decode & encode temperature Thermostat on < 65 off
  • 5. Reactive (Software) Systems Today refers to large software systems as a whole Parts of the system are decoupled in space and time via message passing A reactive software system is a group of reactive systems, each responding to streams of messages Responsive Message Driven ResilientElastic Value Form Means www.reactivemanifesto.org
  • 6. Reactive Program The analog of message passing in a system is eventing in a program Reactive System Responsive Event Driven ResilientElastic Value Form Means
  • 7. Code Code Code Code Code Code streams of events Modeling code components as reactive systems Key Concept #1
  • 8. Observation - Decoupling Code in Time To model a piece of code like a reactive system is to give it the ability to respond to other code Some sort of observation – like a callback, delegate, event, etc. Object A Object B start observing notify notify notify stop observing
  • 9. Observation class A { void processAll() { while(b.hasItems()) { process(b.next()); } } } Class A Class B next() observe() notify() class A { public A() { b.observe(this); } void notify(item) { process(item); } }
  • 10. Declarative vs. Imperative Reactive-style components lend themselves to being declarative Say how things are, not how to do them 5:00 Clock Clock 5:00 5:00 “The time should be here” Each second: - Get the time - Update the view 5:00 time?
  • 11. Declarative Programming 1 2 3 4 A 5 4 = A1 + A2 B Code Spreadsheet var x = 5; var y = 4; var s <=> x + y; // s == 9 x = 9; // s == 13
  • 12. do something Reactive Code Other Code notify Code reacts to streams of events Key Concept #2 Benefits: Reduces dependencies & facilitates declarative code style
  • 13. Where can we apply this? User Interface Logic Datastore REST Client Cloud
  • 14. class ViewModel { void update() { var data = await restClient.Get(); dataStore.Save(data); data = Logic(data); UpdateUI(data); } } User Interface Datastore REST Client Cloud ViewModel Business Logic
  • 15. class ViewModel { void notify(data) { UpdateUI(data); } } User Interface Datastore REST Client Cloud ViewModel Business Logic Provider notify() ViewModel Provider observe entity X notify notify stop observing update X REST call and save update X REST call and save
  • 17. Anything (but not everything) • Closely coupled • Direct method calls • Exchange mutable objects • Decoupled • Observation • Exchange immutable objects
  • 18. Reactive Extensions ■ Create observables and observe them ■ Manipulate the flow of events Observer Observable subscribe() onNext() Op
  • 19. Example var x = 5; var y = 4; var s <=> x + y; // s == 9 x = 9; // s == 13 var x = new Number { Value = 5 }; var y = new Number { Value = 4 }; var s = new Sum(x, y); Console.WriteLine($"s={s.Value}"); x.Value = 9; Console.WriteLine($"s={s.Value}"); https://goo.gl/gn3iTe
  • 20. class Number { private int _value; public int Value { get { return _value; } set { _value = value; _subject.OnNext(_value); } }         private readonly Subject<int> _subject = new Subject<int>();         public IObservable<int> Changed {             get { return _subject.AsObservable(); }         } }
  • 21. class Sum { private int _x; private int _y; private int _sum; public int Value { get { return _sum; } }         public Sum(Number x, Number y)         { _x = x.Value; _y = y.Value;             _sum = _x + _y;             x.Changed.Subscribe((n) => { _x = n; _sum = _x + _y; });             y.Changed.Subscribe((n) => { _y = n; _sum = _x + _y; });         }     }
  • 22. RX Operators – Creation Create an observable that emits a sequence of integers spaced by a given time interval Interval Interval( x sec. ) x sec. x sec. x sec. x sec. 0 1 2 3
  • 23. Suppress duplicate items emitted by an observable Distinct RX Operators – Filtering 1 2 2 1 1 2 3 3 Distinct
  • 24. Transform the items emitted by an Observable by applying a function to each of them Map RX Operators – Transformation 1 2 3 10 20 30 Map( x => 10 * x )
  • 25. Combine multiple Observables into one by merging their emissions Merge RX Operators – Joining 20 40 60 Merge 1 1 80 100 20 40 60 80 1001 1
  • 26. Summary ■ Reactive programming can be thought of as modeling code as individual reactive systems ■ Decoupling code via observation reduces dependencies and facilitates declarative programming ■ Reactive Extensions provide an implementation of the Observer pattern along with operators