An Introduction to Functional
and Reactive Programming
Dan Lew
Slide 2
Slide 2 text
No content
Slide 3
Slide 3 text
Functional Reactive
Programming
Slide 4
Slide 4 text
Reactive Programming
Slide 5
Slide 5 text
No content
Slide 6
Slide 6 text
Proactive Passive
Slide 7
Slide 7 text
public class Switch {
LightBulb lightBulb;
void onFlip(boolean enabled) {
lightBulb.power(enabled);
}
}
Slide 8
Slide 8 text
Observable Reactive
Slide 9
Slide 9 text
public static LightBulb create(Switch theSwitch) {
LightBulb lightBulb = new LightBulb();
theSwitch.addOnFlipListener(enabled -> lightBulb.power(enabled));
return lightBulb;
}
Slide 10
Slide 10 text
No content
Slide 11
Slide 11 text
Proactive Reactive
Who controls LightBulb? Others via power() LightBulb itself
Who determines what Switch
controls?
Switch itself Others via listener
Is LightBulb synchronous? Synchronous Asynchronous
Slide 12
Slide 12 text
Modularity
• Proactive: Modules control each other
• Reactive: Modules control themselves
Slide 13
Slide 13 text
No content
Slide 14
Slide 14 text
No content
Slide 15
Slide 15 text
Why does my db control my UI?
Slide 16
Slide 16 text
No content
Slide 17
Slide 17 text
public class Switch {
interface OnFlipListener {
void onFlip(boolean enabled);
}
void addOnFlipListener(OnFlipListener onFlipListener) {
// ...etc...
}
}
• Every listener unique
• Code cannot be generalized / built-upon
• Every listener requires direct access to Switch
• Listenable not something that can be passed around
Slide 18
Slide 18 text
public class Switch {
??? flips() {
// etc...
}
}
Slide 19
Slide 19 text
Function Returns…
One Many
Sync
Async
Slide 20
Slide 20 text
Function Returns…
One Many
Sync T
Async
Slide 21
Slide 21 text
Function Returns…
One Many
Sync T Iterable
Async
Slide 22
Slide 22 text
Function Returns…
One Many
Sync T Iterable
Async Future
Slide 23
Slide 23 text
Function Returns…
One Many
Sync T Iterable
Async Future Observable
Slide 24
Slide 24 text
public class Switch {
Observable flips() {
// etc...
}
}
// Creating the LightBulb…
public static LightBulb create(Observable switchObs) {
LightBulb lightBulb = new LightBulb();
switchObs.subscribe(enabled -> lightBulb.power(enabled));
return lightBulb;
}
Slide 25
Slide 25 text
public class Switch {
Observable flips() {
// etc...
}
}
// Creating the LightBulb…
public static LightBulb create(Observable observable) {
LightBulb lightBulb = new LightBulb();
switchObs.subscribe(enabled -> lightBulb.power(enabled));
return lightBulb;
}
Slide 26
Slide 26 text
public class Switch {
Observable flips() {
// etc...
}
}
// Creating the LightBulb…
public static LightBulb create(Observable observable) {
LightBulb lightBulb = new LightBulb();
observable.subscribe(enabled -> lightBulb.power(enabled));
return lightBulb;
}
Slide 27
Slide 27 text
public class Switch {
Observable flips() {
// etc...
}
}
// Creating the LightBulb…
public static LightBulb create(Observable observable) {
LightBulb lightBulb = new LightBulb();
observable.subscribe(enabled -> lightBulb.power(enabled));
return lightBulb;
}
Slide 28
Slide 28 text
public class Switch {
Observable flips() {
// etc...
}
}
// Creating the LightBulb…
public static LightBulb create(Observable observable) {
LightBulb lightBulb = new LightBulb();
observable.subscribe(enabled -> lightBulb.power(enabled));
return lightBulb;
}
Slide 29
Slide 29 text
Observable
• Collection over time
• …can have endings…
• …Or errors
Slide 30
Slide 30 text
Functional Programming
Slide 31
Slide 31 text
PURE
Slide 32
Slide 32 text
int two = add(1, 1);
public static int add(int a, int b) {
System.out.println("You're an idiot for using this function!");
System.exit(1010101);
return a + b;
}
Slide 33
Slide 33 text
int two = add(1, 1);
public static int add(int a, int b) {
System.out.println("You're an idiot for using this function!");
System.exit(1010101);
return a + b;
}
Slide 34
Slide 34 text
int two = add(1, 1);
public static int add(int a, int b) {
System.out.println("You're an idiot for using this function!");
System.exit(1010101);
return a + b;
}
Slide 35
Slide 35 text
int two = add(1, 1);
public static int add(int a, int b) {
System.out.println("You're an idiot for using this function!");
System.exit(1010101);
return a + b;
}
Slide 36
Slide 36 text
int two = add(1, 1);
public static int add(int a, int b) {
System.out.println("You're an idiot for using this function!");
System.exit(1010101);
return a + b;
}
Side effects SUCK
Slide 37
Slide 37 text
List numbers = new ArrayList<>(Arrays.asList(1, 2, 3));
boolean sumEqualsProduct = sum(numbers) == product(numbers);
public static int sum(List numbers) {
int total = 0;
Iterator it = numbers.iterator();
while(it.hasNext()) {
total += it.next();
it.remove();
}
return total;
}
Slide 38
Slide 38 text
List numbers = new ArrayList<>(Arrays.asList(1, 2, 3));
boolean sumEqualsProduct = sum(numbers) == product(numbers);
public static int sum(List numbers) {
int total = 0;
Iterator it = numbers.iterator();
while(it.hasNext()) {
total += it.next();
it.remove();
}
return total;
}
Slide 39
Slide 39 text
List numbers = new ArrayList<>(Arrays.asList(1, 2, 3));
boolean sumEqualsProduct = sum(numbers) == product(numbers);
public static int sum(List numbers) {
int total = 0;
Iterator it = numbers.iterator();
while(it.hasNext()) {
total += it.next();
it.remove();
}
return total;
}
Side effects SUCK
Slide 40
Slide 40 text
Side Effects
• No input
List reticulateSplines()
• No output
void consume(Food food)
• Output cannot be derived from input
List getResults(int limit)
• Modifies parameters
void getHitRect(Rect outRect)
Slide 41
Slide 41 text
Functional Programming
• Pure functions
• Immutable data
• Higher-order functions
Slide 42
Slide 42 text
public static List doubleValues(List input) {
List output = new ArrayList<>();
for (Integer value : input) {
output.add(value * 2);
}
return output;
}
Inflexible!
Slide 43
Slide 43 text
public interface Function {
Integer apply(Integer int);
}
public static List map(List input,
Function fun) {
List output = new ArrayList<>();
for (Integer value : input) {
output.add(fun.apply(value));
}
return output;
}
Slide 44
Slide 44 text
public interface Function {
Integer apply(Integer int);
}
public static List map(List input,
Function fun) {
List output = new ArrayList<>();
for (Integer value : input) {
output.add(fun.apply(value));
}
return output;
}
Slide 45
Slide 45 text
public interface Function {
Integer apply(Integer int);
}
public static List map(List input,
Function fun) {
List output = new ArrayList<>();
for (Integer value : input) {
output.add(fun.apply(value));
}
return output;
}
Slide 46
Slide 46 text
List numbers = Arrays.asList(1, 2, 3);
List doubled = map(numbers, i -> i * 2);
Slide 47
Slide 47 text
public interface Function {
R apply(T t);
}
public static List map(List input,
Function fun) {
List output = new ArrayList<>();
for (T value : input) {
output.add(fun.apply(value));
}
return output;
}
Slide 48
Slide 48 text
List numbers = Arrays.asList(1, 2, 3);
List doubled = map(numbers, i -> i * 2);
List words = Arrays.asList("one", "two", "three");
List lengths = map(words, s -> s.length());
Slide 49
Slide 49 text
List words = Arrays.asList("one", "two", "three");
List lengths = map(words, s -> s.length());
Slide 50
Slide 50 text
Back to reactive land…
Slide 51
Slide 51 text
public class Switch {
enum State {
ON,
OFF
}
Observable flips() {
// etc...
}
}
// Creating the LightBulb…
public static LightBulb create(Observable switchObs) {
LightBulb lightBulb = new LightBulb();
switchObs.subscribe(enabled -> lightBulb.power(enabled));
return lightBulb;
}
Slide 52
Slide 52 text
public class Switch {
enum State {
ON,
OFF
}
Observable flips() {
// etc...
}
}
// Creating the LightBulb…
public static LightBulb create(Observable switchObs) {
LightBulb lightBulb = new LightBulb();
switchObs.subscribe(enabled -> lightBulb.power(enabled));
return lightBulb;
}
Slide 53
Slide 53 text
public class Switch {
enum State {
ON,
OFF
}
Observable flips() {
// etc...
}
}
// Creating the LightBulb…
public static LightBulb create(Observable observable) {
LightBulb lightBulb = new LightBulb();
observable.subscribe(enabled -> lightBulb.power(enabled));
return lightBulb;
}
Slide 54
Slide 54 text
No content
Slide 55
Slide 55 text
Switch theSwitch = new Switch();
Observable stateObservable = theSwitch.flips();
Observable booleanObservable = stateObservable
.map(state -> state == State.ON);
LightBulb.create(booleanObservable);
Slide 56
Slide 56 text
Switch theSwitch = new Switch();
Observable stateObservable = theSwitch.flips();
Observable booleanObservable = stateObservable
.map(state -> state == State.ON);
LightBulb.create(booleanObservable);
Slide 57
Slide 57 text
Switch theSwitch = new Switch();
Observable stateObservable = theSwitch.flips();
Observable booleanObservable = stateObservable
.map(state -> state == State.ON);
LightBulb.create(booleanObservable);
Slide 58
Slide 58 text
Switch theSwitch = new Switch();
Observable stateObservable = theSwitch.flips();
Observable booleanObservable = stateObservable
.map(state -> state == State.ON);
LightBulb.create(booleanObservable);
Slide 59
Slide 59 text
Switch theSwitch = new Switch();
Observable stateObservable = theSwitch.flips();
Observable booleanObservable = stateObservable
.map(state -> state == State.ON);
LightBulb.create(booleanObservable);