Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Functional Reactive Programming

Daniel Lew
February 04, 2017

Introduction to Functional Reactive Programming

Talk given at GDG DevFest MN 2017. (Recording may be available someday.)

Daniel Lew

February 04, 2017
Tweet

More Decks by Daniel Lew

Other Decks in Programming

Transcript

  1. public static LightBulb create(Switch theSwitch) {
 LightBulb lightBulb = new

    LightBulb();
 theSwitch.addOnFlipListener(enabled -> lightBulb.power(enabled));
 return lightBulb;
 }
  2. 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
  3. 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
  4. 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;
 }
  5. 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;
 }
  6. 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;
 }
  7. 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;
 }
  8. 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;
 }
  9. 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;
 }
  10. 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;
 }
  11. 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;
 }
  12. 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;
 }
  13. 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
  14. 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;
 }
  15. 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;
 }
  16. 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
  17. 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)
  18. public static List<Integer> doubleValues(List<Integer> input) {
 List<Integer> output = new

    ArrayList<>();
 for (Integer value : input) {
 output.add(value * 2);
 }
 return output;
 } Inflexible!
  19. 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;
 }
  20. 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;
 }
  21. 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;
 }
  22. 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;
 }
  23. 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());
  24. 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;
 }
  25. 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;
 }
  26. 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;
 }
  27. 
 Switch theSwitch = new Switch(); Observable<State> stateObservable = theSwitch.flips();

    
 Observable<Boolean> booleanObservable = stateObservable
 .map(state -> state == State.ON); 
 LightBulb.create(booleanObservable);
  28. 
 Switch theSwitch = new Switch(); 
 Observable<State> stateObservable =

    theSwitch.flips(); 
 Observable<Boolean> booleanObservable = stateObservable
 .map(state -> state == State.ON); 
 LightBulb.create(booleanObservable);
  29. 
 Switch theSwitch = new Switch(); 
 Observable<State> stateObservable =

    theSwitch.flips(); 
 Observable<Boolean> booleanObservable = stateObservable
 .map(state -> state == State.ON); 
 LightBulb.create(booleanObservable);
  30. 
 Switch theSwitch = new Switch(); 
 Observable<State> stateObservable =

    theSwitch.flips(); 
 Observable<Boolean> booleanObservable = stateObservable
 .map(state -> state == State.ON); 
 LightBulb.create(booleanObservable);
  31. 
 Switch theSwitch = new Switch(); 
 Observable<State> stateObservable =

    theSwitch.flips(); 
 Observable<Boolean> booleanObservable = stateObservable
 .map(state -> state == State.ON); 
 LightBulb.create(booleanObservable);
  32. • 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
  33. But why? • Reactive streams • Modularity • Inherently asynchronous

    • Functional operators • Control flow of streams • Reproduce common logic