Share
Embed
Introduction to Functional Reactive Programming
Talk given at GDG DevFest MN 2017. (Recording may be available someday.)
Daniel Lew
February 04, 2017
Transcript
67 slides
-
public class Switch { LightBulb lightBulb; void onFlip(boolean enabled)
{ lightBulb.power(enabled); } } -
public static LightBulb create(Switch theSwitch) { LightBulb lightBulb = new
LightBulb(); theSwitch.addOnFlipListener(enabled -> lightBulb.power(enabled)); return lightBulb; } -
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 -
Modularity • Proactive: Modules control each other • Reactive: Modules
control themselves -
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 -
public class Switch { Observable<Boolean> flips() { // etc... }
} // Creating the LightBulb… public static LightBulb create(Observable<Boolean> switchObs) { LightBulb lightBulb = new LightBulb(); switchObs.subscribe(enabled -> lightBulb.power(enabled)); return lightBulb; } -
public class Switch { Observable<Boolean> flips() { // etc... }
} // Creating the LightBulb… public static LightBulb create(Observable<Boolean> observable) { LightBulb lightBulb = new LightBulb(); switchObs.subscribe(enabled -> lightBulb.power(enabled)); return lightBulb; } -
public class Switch { Observable<Boolean> flips() { // etc... }
} // Creating the LightBulb… public static LightBulb create(Observable<Boolean> observable) { LightBulb lightBulb = new LightBulb(); observable.subscribe(enabled -> lightBulb.power(enabled)); return lightBulb; } -
public class Switch { Observable<Boolean> flips() { // etc... }
} // Creating the LightBulb… public static LightBulb create(Observable<Boolean> observable) { LightBulb lightBulb = new LightBulb(); observable.subscribe(enabled -> lightBulb.power(enabled)); return lightBulb; } -
public class Switch { Observable<Boolean> flips() { // etc... }
} // Creating the LightBulb… public static LightBulb create(Observable<Boolean> observable) { LightBulb lightBulb = new LightBulb(); observable.subscribe(enabled -> lightBulb.power(enabled)); return lightBulb; } -
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; } -
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; } -
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; } -
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; } -
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 -
List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3)); boolean sumEqualsProduct =
sum(numbers) == product(numbers); public static int sum(List<Integer> numbers) { int total = 0; Iterator<Integer> it = numbers.iterator(); while(it.hasNext()) { total += it.next(); it.remove(); } return total; } -
List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3)); boolean sumEqualsProduct =
sum(numbers) == product(numbers); public static int sum(List<Integer> numbers) { int total = 0; Iterator<Integer> it = numbers.iterator(); while(it.hasNext()) { total += it.next(); it.remove(); } return total; } -
List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3)); boolean sumEqualsProduct =
sum(numbers) == product(numbers); public static int sum(List<Integer> numbers) { int total = 0; Iterator<Integer> it = numbers.iterator(); while(it.hasNext()) { total += it.next(); it.remove(); } return total; } Side effects SUCK -
Side Effects • No input List<Spline> reticulateSplines() • No output
void consume(Food food) • Output cannot be derived from input List<String> getResults(int limit) • Modifies parameters void getHitRect(Rect outRect) -
public static List<Integer> doubleValues(List<Integer> input) { List<Integer> output = new
ArrayList<>(); for (Integer value : input) { output.add(value * 2); } return output; } Inflexible! -
public interface Function { Integer apply(Integer int); } public
static List<Integer> map(List<Integer> input, Function<Integer, Integer> fun) { List<Integer> output = new ArrayList<>(); for (Integer value : input) { output.add(fun.apply(value)); } return output; } -
public interface Function { Integer apply(Integer int); } public
static List<Integer> map(List<Integer> input, Function fun) { List<Integer> output = new ArrayList<>(); for (Integer value : input) { output.add(fun.apply(value)); } return output; } -
public interface Function { Integer apply(Integer int); } public
static List<Integer> map(List<Integer> input, Function fun) { List<Integer> output = new ArrayList<>(); for (Integer value : input) { output.add(fun.apply(value)); } return output; } -
public interface Function<T, R> { R apply(T t); }
public static <T, R> List<R> map(List<T> input, Function<T, R> fun) { List<R> output = new ArrayList<>(); for (T value : input) { output.add(fun.apply(value)); } return output; } -
List<Integer> numbers = Arrays.asList(1, 2, 3); List<Integer> doubled = map(numbers,
i -> i * 2); List<String> words = Arrays.asList("one", "two", "three"); List<Integer> lengths = map(words, s -> s.length()); -
public class Switch { enum State { ON, OFF }
Observable<State> flips() { // etc... } } // Creating the LightBulb… public static LightBulb create(Observable<Boolean> switchObs) { LightBulb lightBulb = new LightBulb(); switchObs.subscribe(enabled -> lightBulb.power(enabled)); return lightBulb; } -
public class Switch { enum State { ON, OFF }
Observable<State> flips() { // etc... } } // Creating the LightBulb… public static LightBulb create(Observable<Boolean> switchObs) { LightBulb lightBulb = new LightBulb(); switchObs.subscribe(enabled -> lightBulb.power(enabled)); return lightBulb; } -
public class Switch { enum State { ON, OFF }
Observable<State> flips() { // etc... } } // Creating the LightBulb… public static LightBulb create(Observable<Boolean> observable) { LightBulb lightBulb = new LightBulb(); observable.subscribe(enabled -> lightBulb.power(enabled)); return lightBulb; } -
Switch theSwitch = new Switch(); Observable<State> stateObservable = theSwitch.flips();
Observable<Boolean> booleanObservable = stateObservable .map(state -> state == State.ON); LightBulb.create(booleanObservable); -
Switch theSwitch = new Switch(); Observable<State> stateObservable =
theSwitch.flips(); Observable<Boolean> booleanObservable = stateObservable .map(state -> state == State.ON); LightBulb.create(booleanObservable); -
Switch theSwitch = new Switch(); Observable<State> stateObservable =
theSwitch.flips(); Observable<Boolean> booleanObservable = stateObservable .map(state -> state == State.ON); LightBulb.create(booleanObservable); -
Switch theSwitch = new Switch(); Observable<State> stateObservable =
theSwitch.flips(); Observable<Boolean> booleanObservable = stateObservable .map(state -> state == State.ON); LightBulb.create(booleanObservable); -
Switch theSwitch = new Switch(); Observable<State> stateObservable =
theSwitch.flips(); Observable<Boolean> booleanObservable = stateObservable .map(state -> state == State.ON); LightBulb.create(booleanObservable); -
• create • defer • from • interval • just
• range • repeat • timer • buffer • map • flatMap • switchMap • groupBy • scan • reduce • window • debounce • distinct • elementAt • filter • first • last • ignoreElements • sample • skip • skipLast • skipWhile • take • takeList • takeUntil • combineLatest • zip • merge • concat • amb • startWith • do • observeOn • subscribeOn • delay • publish • throttle • timestamp • retry -
But why? • Reactive streams • Modularity • Inherently asynchronous
• Functional operators • Control flow of streams • Reproduce common logic -
Resources • RxJava introduction: http://blog.danlew.net/2014/09/15/grokking- rxjava-part-1/ • Stream explanation: https://cycle.js.org/streams.html
• Reactive duality: https://goo.gl/F2OyVp • Learn you a Haskell: learnyouahaskell.com