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

Intro to Reactive Programming and RxJava: How to React 😮😱🤪

Intro to Reactive Programming and RxJava: How to React 😮😱🤪

Approach programming in a new way by using Reactive Programming, a functional style that focuses on the flow and manipulation of data. You already know how to think imperatively, using if-statements and state variables, from your experience programming in C. We will discuss the history of Imperative and Reactive programming and compare the different styles with concrete code examples in Java. By the end of this talk you will understand when to use reactive programming and how powerful and succinct it is for data problems.

Jordan Jozwiak

October 01, 2018
Tweet

More Decks by Jordan Jozwiak

Other Decks in Technology

Transcript

  1. Intro to Reactive Programming and RxJava:
    How to React
    Jordan Jozwiak

    View Slide

  2. Reactive
    Programming
    Allows us to manipulate a
    stream of data with functions.


    View Slide

  3. Reactive
    Programming
    Allows us to manipulate a
    stream of data with functions.

    (asynchronously)

    View Slide

  4. Reactive
    Programming

    View Slide

  5. Reactive
    Programming
    1. Data streams


    View Slide

  6. Reactive
    Programming
    1. Data streams

    2. Functional Programming


    View Slide

  7. Reactive
    Programming
    1. Data streams

    2. Functional Programming

    3. Asynchronous observers

    View Slide

  8. applications
    ! Retrieving data from a database and then filtering out some results
    based on user settings.

    View Slide

  9. applications
    ! Retrieving data from a database and then filtering out some results
    based on user settings.
    2 3 7

    View Slide

  10. applications
    ! Retrieving data from a database and then filtering out some results
    based on user settings.

    View Slide

  11. applications
    ! Retrieving data from a database and then filtering out some results
    based on user settings.
    a, b, c -> a, c

    View Slide

  12. applications
    ! Retrieve data from a database and then filtering out some results
    based on user settings.
    ! Render a UI that combines data from multiple data sources
    ! Create a real-time model for stock prices
    ! Show auto-complete search results to a user
    ! Log data from a temperature, wind, or pressure sensor

    View Slide

  13. Who actually uses this?

    View Slide

  14. Who actually uses this?
    ! Very popular in the Android community

    View Slide

  15. Who actually uses this?
    ! Very popular in the Android community
    ! Some usage at Google for database requests

    View Slide

  16. Who actually uses this?
    ! Very popular in the Android community
    ! Some usage at Google for database requests
    ! Microsoft, Netflix, GitHub, SoundCloud, and more (see reactivex.io)

    View Slide

  17. Reactive Programming
    Theory

    View Slide

  18. Reactive Programming
    Theory
    Implementation
    Reactive Extensions (reactivex.io)
    (libraries to make normal languages reactive)

    View Slide

  19. Reactive Programming
    Theory
    Implementation
    Reactive Extensions (reactivex.io)
    (libraries to make normal languages reactive)
    RxJava RxSwift RxJS …

    View Slide

  20. View Slide

  21. 4 2 1 7 6

    View Slide

  22. 4 2 1 7 6
    filter(x -> (x % 2) == 0)

    View Slide

  23. 4 2 1 7 6
    filter(x -> (x % 2) == 0)

    View Slide

  24. 4 2 1 7 6
    filter(x -> (x % 2) == 0)
    4

    View Slide

  25. 4 2 1 7 6
    filter(x -> (x % 2) == 0)
    4 2

    View Slide

  26. 4 2 1 7 6
    filter(x -> (x % 2) == 0)
    4 2 6

    View Slide

  27. 4 2 1 7 6
    filter(x -> (x % 2) == 0)
    4 2 6

    View Slide

  28. 4 2 1 7 6
    SUBJECT
    subject - the stream that emits data
    4 2 6

    View Slide

  29. 4 2 6
    OBSERVER
    observer - “subscribes to” or “consumes” data. There
    can be multiple observers for a single subject.
    4 2 1 7 6

    View Slide

  30. 4 2 1 7 6
    filter(x -> (x % 2) == 0)
    4 2 6
    SUBJECT
    OBSERVER

    View Slide

  31. 4 2 1 7 6
    filter(x -> (x % 2) == 0)
    4 2 6
    SUBJECT
    OBSERVER
    4 2 6
    SERVER 2

    View Slide

  32. 4 2 1 7 6
    filter / map / count / 

    max / min / delay / ...
    SUBJECT
    OBSERVER

    View Slide

  33. 4 2 1 7 6
    some function that takes ⏳
    SUBJECT
    OBSERVER

    View Slide

  34. 4 2 1 7 6
    some function that takes ⏳
    SUBJECT
    OBSERVER

    View Slide

  35. a d z a h
    some function that takes ⏳
    SUBJECT
    OBSERVER

    View Slide

  36. some function that takes ⏳
    SUBJECT
    OBSERVER

    View Slide

  37. ? ? ? ? ?
    some function that takes ⏳
    ? ? ? ?
    SUBJECT
    OBSERVER

    View Slide

  38. some function that takes ⏳
    SUBJECT
    OBSERVER
    another function

    View Slide

  39. ? ? ? ? ?
    some function that takes ⏳
    ? ? ? ?
    SUBJECT
    OBSERVER
    another function
    ? ? ? ? ?

    View Slide

  40. What creates the subject data stream?

    View Slide

  41. What creates the subject data stream?
    ! These streams are called Observables in RxJava

    View Slide

  42. What creates the subject data stream?
    ! These streams are called Observables in RxJava
    ! Observable.create(), fromIterable(), etc.

    View Slide

  43. What creates the subject data stream?
    ! These streams are called Observables in RxJava
    ! Observable.create(), fromIterable(), etc.
    ! Retrofitting existing object-oriented design into Observable data
    streams can be messy

    View Slide

  44. What creates the subject data stream?
    ! These streams are called Observables in RxJava
    ! Observable.create(), fromIterable(), etc.
    ! Retrofitting existing object-oriented design into Observable data
    streams can be messy
    ! Many, many open-source projects that wrap other implementations into
    an Rx package (an easy way to get ⭐ ⭐ ⭐ on GitHub)

    View Slide

  45. What creates the subject data stream?
    ! These streams are called Observables in RxJava
    ! Observable.create(), fromIterable(), etc.
    ! Retrofitting existing object-oriented design into Observable data
    streams can be messy
    ! Many, many open-source projects that wrap other implementations into
    an Rx package (an easy way to get ⭐ ⭐ ⭐ on GitHub)
    ! github.com/zsoltk/RxAndroidLibs - 500 stars for a list of libraries

    View Slide

  46. introduction
    background
    examples
    live demo
    wrap-up

    View Slide

  47. What languages
    are you familiar with?

    View Slide

  48. ~80 %
    Percent of the most popular languages are imperative 

    or multi-paradigmatic languages
    Source: StackOverflow Developer Survey 2018

    View Slide

  49. What's a language paradigm?

    View Slide

  50. What's a language paradigm?
    ! Computer languages resemble linguistic language

    View Slide

  51. What's a language paradigm?
    ! Computer languages resemble linguistic language
    ○ Grammatical structures

    View Slide

  52. What's a language paradigm?
    ! Computer languages resemble linguistic language
    ○ Grammatical structures
    ○ Tenses: past, present, future

    View Slide

  53. What's a language paradigm?
    ! Computer languages resemble linguistic language
    ○ Grammatical structures
    ○ Tenses: past, present, future
    ○ Vocabulary

    View Slide

  54. What's a language paradigm?
    ! Computer languages resemble linguistic language
    ○ Grammatical structures
    ○ Tenses: past, present, future
    ○ Vocabulary
    ○ Dialects

    View Slide

  55. What's a language paradigm?
    ! Computer languages resemble linguistic language
    ○ Grammatical structures
    ○ Tenses: past, present, future
    ○ Vocabulary
    ○ Dialects
    ! Languages are multi-faceted and evolve over time. They are often multi-
    paradigmatic languages.

    View Slide

  56. What's a language paradigm?
    ! Computer languages resemble linguistic language
    ○ Grammatical structures
    ○ Tenses: past, present, future
    ○ Vocabulary
    ○ Dialects
    ! Languages are multi-faceted and evolve over time. They are often multi-
    paradigmatic languages.
    ! Examples

    View Slide

  57. What's a language paradigm?
    ! Computer languages resemble linguistic language
    ○ Grammatical structures
    ○ Tenses: past, present, future
    ○ Vocabulary
    ○ Dialects
    ! Languages are multi-faceted and evolve over time. They are often multi-
    paradigmatic languages.
    ! Examples
    ○ Imperative (what you’re probably used to)

    View Slide

  58. What's a language paradigm?
    ! Computer languages resemble linguistic language
    ○ Grammatical structures
    ○ Tenses: past, present, future
    ○ Vocabulary
    ○ Dialects
    ! Languages are multi-faceted and evolve over time. They are often multi-
    paradigmatic languages.
    ! Examples
    ○ Imperative (what you’re probably used to)
    ○ Reactive Programming (what we’re talking about today)

    View Slide

  59. What's a language paradigm?
    ! Computer languages resemble linguistic language
    ○ Grammatical structures
    ○ Tenses: past, present, future
    ○ Vocabulary
    ○ Dialects
    ! Languages are multi-faceted and evolve over time. They are often multi-
    paradigmatic languages.
    ! Examples
    ○ Imperative (what you’re probably used to)
    ○ Reactive Programming (what we’re talking about today)
    ○ Functional Programming (used in Reactive Programming)

    View Slide

  60. Imperative vs. Functional

    View Slide

  61. Imperative vs. Functional
    Imperative Programming

    View Slide

  62. Imperative vs. Functional
    Imperative Programming
    ! Describes how the program
    operates

    View Slide

  63. Imperative vs. Functional
    Imperative Programming
    ! Describes how the program
    operates
    ! Uses statements to change
    state of the program

    View Slide

  64. Imperative vs. Functional
    Imperative Programming
    ! Describes how the program
    operates
    ! Uses statements to change
    state of the program
    ! Conditional statements, loops,
    inheritance

    View Slide

  65. Imperative vs. Functional
    Imperative Programming
    ! Describes how the program
    operates
    ! Uses statements to change
    state of the program
    ! Conditional statements, loops,
    inheritance
    ! Easier initial learning curve

    View Slide

  66. Imperative vs. Functional
    Imperative Programming
    ! Describes how the program
    operates
    ! Uses statements to change
    state of the program
    ! Conditional statements, loops,
    inheritance
    ! Easier initial learning curve
    ! Examples: C, PHP, Java

    View Slide

  67. Imperative vs. Functional
    Imperative Programming
    ! Describes how the program
    operates
    ! Uses statements to change
    state of the program
    ! Conditional statements, loops,
    inheritance
    ! Easier initial learning curve
    ! Examples: C, PHP, Java
    if (value % 2 == 0) {
    // do something
    }

    View Slide

  68. Imperative vs. Functional
    Imperative Programming
    ! Describes how the program
    operates
    ! Uses statements to change
    state of the program
    ! Conditional statements, loops,
    inheritance
    ! Easier initial learning curve
    ! Examples: C, PHP, Java
    Functional Programming
    if (value % 2 == 0) {
    // do something
    }

    View Slide

  69. Imperative vs. Functional
    Imperative Programming
    ! Describes how the program
    operates
    ! Uses statements to change
    state of the program
    ! Conditional statements, loops,
    inheritance
    ! Easier initial learning curve
    ! Examples: C, PHP, Java
    Functional Programming
    ! Describes what the program
    should accomplish
    if (value % 2 == 0) {
    // do something
    }

    View Slide

  70. Imperative vs. Functional
    Imperative Programming
    ! Describes how the program
    operates
    ! Uses statements to change
    state of the program
    ! Conditional statements, loops,
    inheritance
    ! Easier initial learning curve
    ! Examples: C, PHP, Java
    Functional Programming
    ! Describes what the program
    should accomplish
    ! Ideally stateless
    if (value % 2 == 0) {
    // do something
    }

    View Slide

  71. Imperative vs. Functional
    Imperative Programming
    ! Describes how the program
    operates
    ! Uses statements to change
    state of the program
    ! Conditional statements, loops,
    inheritance
    ! Easier initial learning curve
    ! Examples: C, PHP, Java
    Functional Programming
    ! Describes what the program
    should accomplish
    ! Ideally stateless
    ! Evaluate functions and create
    expressions instead of
    statements
    if (value % 2 == 0) {
    // do something
    }

    View Slide

  72. Imperative vs. Functional
    Imperative Programming
    ! Describes how the program
    operates
    ! Uses statements to change
    state of the program
    ! Conditional statements, loops,
    inheritance
    ! Easier initial learning curve
    ! Examples: C, PHP, Java
    Functional Programming
    ! Describes what the program
    should accomplish
    ! Ideally stateless
    ! Evaluate functions and create
    expressions instead of
    statements
    ! More difficult conceptually
    if (value % 2 == 0) {
    // do something
    }

    View Slide

  73. Imperative vs. Functional
    Imperative Programming
    ! Describes how the program
    operates
    ! Uses statements to change
    state of the program
    ! Conditional statements, loops,
    inheritance
    ! Easier initial learning curve
    ! Examples: C, PHP, Java
    Functional Programming
    ! Describes what the program
    should accomplish
    ! Ideally stateless
    ! Evaluate functions and create
    expressions instead of
    statements
    ! More difficult conceptually
    ! Examples: Scala, Haskell
    if (value % 2 == 0) {
    // do something
    }

    View Slide

  74. Imperative vs. Functional
    Imperative Programming
    ! Describes how the program
    operates
    ! Uses statements to change
    state of the program
    ! Conditional statements, loops,
    inheritance
    ! Easier initial learning curve
    ! Examples: C, PHP, Java
    Functional Programming
    ! Describes what the program
    should accomplish
    ! Ideally stateless
    ! Evaluate functions and create
    expressions instead of
    statements
    ! More difficult conceptually
    ! Examples: Scala, Haskell
    if (value % 2 == 0) {
    // do something
    }
    boolean filter(int value) {
    return value % 2 == 0;
    }

    View Slide

  75. History of Imperative Programming
    ! Old computers utilized vacuum tubes and very limited memory
    ! Programs written as step-by-step instructions
    ! FORTRAN introduced in 1954. Compiled, variables, complex
    expressions.
    ! 60s and 70s: COBOL, BASIC, C
    ! Object-oriented code grew in popularity in the 1980s. Smalltalk. C++
    ! 90s: Perl, Python, Javascript, Ruby, Java

    View Slide

  76. History of Functional Programming
    ! Based on lambda calculus (Church 1936)
    ○ Study of computation with functions
    ! Lisp was developed by IBM in the 1950s
    ○ LISP = LISt Processor
    ○ Still in use today
    ○ Only a year younger than FORTRAN
    ! John Backus gave a 1978 Turing Award lecture on why imperative programming is
    “bad” and functional programming is “good”
    ! 80s-00s - Object-Oriented programming dominated based on principles like
    encapsulation. Academics and theoreticians care about Functional Programming.
    ! 2010s - JavaScript made passing around functions cool again

    View Slide

  77. Reactive Programming

    1. Data streams


    2. Functional programming


    3. Asynchronous observers


    View Slide

  78. Reactive Programming

    1. Data streams


    2. Functional programming


    3. Asynchronous observers


    View Slide

  79. A flow of data objects from one
    function to the next
    Reactive Programming

    1. Data streams


    2. Functional programming


    3. Asynchronous observers


    View Slide

  80. Reactive Programming

    1. Data streams


    2. Functional programming


    3. Asynchronous observers


    View Slide

  81. ! Functions are meant to be pure
    Reactive Programming

    1. Data streams


    2. Functional programming


    3. Asynchronous observers


    View Slide

  82. ! Functions are meant to be pure
    ! All functions have an input and
    output data types
    Reactive Programming

    1. Data streams


    2. Functional programming


    3. Asynchronous observers


    View Slide

  83. ! Functions are meant to be pure
    ! All functions have an input and
    output data types
    ! No external state is modified
    (no side-effects)
    Reactive Programming

    1. Data streams


    2. Functional programming


    3. Asynchronous observers


    View Slide

  84. ! Functions are meant to be pure
    ! All functions have an input and
    output data types
    ! No external state is modified
    (no side-effects)
    Example:

    add(x, y) -> return x + y
    Reactive Programming

    1. Data streams


    2. Functional programming


    3. Asynchronous observers


    View Slide

  85. Reactive Programming

    1. Data streams


    2. Functional programming


    3. Asynchronous observers


    View Slide

  86. Uses the Observer pattern with
    Subject data sources that notify
    Observers
    Reactive Programming

    1. Data streams


    2. Functional programming


    3. Asynchronous observers


    View Slide

  87. Uses the Observer pattern with
    Subject data sources that notify
    Observers
    Errors propagated through the flow
    of data
    Reactive Programming

    1. Data streams


    2. Functional programming


    3. Asynchronous observers


    View Slide

  88. Reactive Programming is NOT

    View Slide

  89. Reactive Programming is NOT
    ! It's own language
    ! React, ReactJs, ReactNative

    View Slide

  90. RxJava

    View Slide

  91. RxJava
    ! The Java library for the Reactive Extensions specification
    ○ This specification is applied to other languages: RxJs, RxSwift,
    etc. - reactivex.io/languages

    View Slide

  92. RxJava
    ! The Java library for the Reactive Extensions specification
    ○ This specification is applied to other languages: RxJs, RxSwift,
    etc. - reactivex.io/languages
    ! Helpful due to complicated stream and asynchronous apis

    View Slide

  93. RxJava
    ! The Java library for the Reactive Extensions specification
    ○ This specification is applied to other languages: RxJs, RxSwift,
    etc. - reactivex.io/languages
    ! Helpful due to complicated stream and asynchronous apis
    ! Brings functional design to an object-oriented language

    View Slide

  94. RxJava
    ! The Java library for the Reactive Extensions specification
    ○ This specification is applied to other languages: RxJs, RxSwift,
    etc. - reactivex.io/languages
    ! Helpful due to complicated stream and asynchronous apis
    ! Brings functional design to an object-oriented language
    ! Very helpful for mobile development, which often involves
    asynchronously requesting data from a server, transforming it, and
    displaying it in the UI

    View Slide

  95. introduction
    background
    examples
    live demo
    wrap-up

    View Slide

  96. ... to subscribe to data
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    System.out.println(value);
    }
    Output: 4, 2, 1, 7, 6, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  97. ... to subscribe to data
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    System.out.println(value);
    }
    Output: 4, 2, 1, 7, 6, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  98. ... to subscribe to data
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    System.out.println(value);
    }
    Output: 4, 2, 1, 7, 6, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  99. ... to subscribe to data
    Reactive
    new DataSource().getDataStream()
    .subscribe(new Consumer() {
    @Override
    public void accept(Integer value) {
    System.out.println(value);
    }
    });
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    System.out.println(value);
    }
    Output: 4, 2, 1, 7, 6, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  100. ... to subscribe to data
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    System.out.println(value);
    }
    Reactive
    new DataSource().getDataStream()
    .subscribe(new Consumer() {
    @Override
    public void accept(Integer value) {
    System.out.println(value);
    }
    });
    Output: 4, 2, 1, 7, 6, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  101. ... to subscribe to data
    Reactive
    new DataSource().getDataStream()
    .subscribe(new Consumer() {
    @Override
    public void accept(Integer value) {
    System.out.println(value);
    }
    });
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    System.out.println(value);
    }
    Output: 4, 2, 1, 7, 6, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  102. ... to subscribe to data
    Reactive
    new DataSource().getDataStream()
    .subscribe(new Consumer() {
    @Override
    public void accept(Integer value) {
    System.out.println(value);
    }
    });
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    System.out.println(value);
    }
    Output: 4, 2, 1, 7, 6, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  103. ... to subscribe to data
    Reactive
    new DataSource().getDataStream()
    .subscribe(new Consumer() {
    @Override
    public void accept(Integer value) {
    System.out.println(value);
    }
    });
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    System.out.println(value);
    }
    Output: 4, 2, 1, 7, 6, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  104. ... to subscribe to data
    Reactive
    new DataSource().getDataStream()
    .subscribe(new Consumer() {
    @Override
    public void accept(Integer value) {
    System.out.println(value);
    }
    });
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    System.out.println(value);
    }
    Output: 4, 2, 1, 7, 6, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  105. ... to subscribe to data
    Reactive
    new DataSource().getDataStream()
    .subscribe(

    value -> System.out.println(value)

    );
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    System.out.println(value);
    }
    Output: 4, 2, 1, 7, 6, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  106. ... to subscribe to data
    Reactive
    new DataSource().getDataStream()
    .subscribe(
    value -> System.out.println(value)
    );
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    System.out.println(value);
    }
    Output: 4, 2, 1, 7, 6, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  107. ... to subscribe to data
    Reactive
    new DataSource().getDataStream()
    .subscribe(
    value -> updateUI(value)
    );
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    updateUI(value);
    }

    View Slide

  108. ... to subscribe to data
    Reactive
    new DataSource().getDataStream()
    .subscribe(
    value -> saveToDatabase(value)
    );
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    saveToDatabase(value);
    }

    View Slide

  109. ... to subscribe to data changes

    View Slide

  110. ... to subscribe to data changes
    Imperative
    ScheduledExecutorService scheduler = // initialize
    Runnable runner = new Runnable() {
    public void run() {
    List data = new DataSource().getData();
    for (Integer value : data) {
    System.out.println(value);
    }
    }
    };
    ScheduledFuture> future = // initialize
    scheduler.schedule(new Runnable() {
    public void run() {
    future.cancel(/* mayInterruptIfRunning */ true);
    }
    }, /* delay */ 0, TimeUnit.SECONDS);

    View Slide

  111. ... to subscribe to data changes
    Imperative
    ScheduledExecutorService scheduler = // initialize
    Runnable runner = new Runnable() {
    public void run() {
    List data = new DataSource().getData();
    for (Integer value : data) {
    System.out.println(value);
    }
    }
    };
    ScheduledFuture> future = // initialize
    scheduler.schedule(new Runnable() {
    public void run() {
    future.cancel(/* mayInterruptIfRunning */ true);
    }
    }, /* delay */ 0, TimeUnit.SECONDS);
    ???

    View Slide

  112. ... to subscribe to data changes
    Imperative
    ScheduledExecutorService scheduler = // initialize
    Runnable runner = new Runnable() {
    public void run() {
    List data = new DataSource().getData();
    for (Integer value : data) {
    System.out.println(value);
    }
    }
    };
    ScheduledFuture> future = // initialize
    scheduler.schedule(new Runnable() {
    public void run() {
    future.cancel(/* mayInterruptIfRunning */ true);
    }
    }, /* delay */ 0, TimeUnit.SECONDS);
    Reactive
    new DataSource().getDataStream()
    .subscribe(
    value -> System.out.println(value)
    );

    View Slide

  113. ... to filter our data

    View Slide

  114. ... to filter our data
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    if (value % 2 == 0) {
    System.out.println(value);
    }
    }
    Output: 4, 2, 6, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  115. ... to filter our data
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    if (value % 2 == 0) {
    System.out.println(value);
    }
    }
    Output: 4, 2, 6, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  116. ... to filter our data
    http://reactivex.io/documentation/operators/filter

    View Slide

  117. ... to filter our data
    Reactive
    new DataSource().getDataStream()
    .filter(value -> value % 2 == 0)
    .subscribe(

    result -> System.out.println(result)

    );
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    if (value % 2 == 0) {
    System.out.println(value);
    }
    }
    Output: 4, 2, 6, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  118. ... to filter our data
    Reactive
    new DataSource().getDataStream()
    .filter(value -> value % 2 == 0)
    .subscribe(

    result -> System.out.println(result)

    );
    Imperative
    List data = 

    new DataSource().getData();
    for (Integer value : data) {
    if (value % 2 == 0) {
    System.out.println(value);
    }
    }
    Output: 4, 2, 6, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  119. … to map our data
    Output: 8, 4, 2, 14, 12, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  120. … to map our data
    Imperative
    List data =
    new DataSource().getData();
    for (Integer value : data) {
    int result = value * 2;
    System.out.println(result);
    }
    Output: 8, 4, 2, 14, 12, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  121. ... to map our data
    Imperative
    List data =
    new DataSource().getData();
    for (Integer value : data) {
    int result = value * 2;
    System.out.println(result);
    }
    Output: 8, 4, 2, 14, 12, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  122. ... to map our data
    http://reactivex.io/documentation/operators/map

    View Slide

  123. ... to map our data
    Reactive
    new DataSource().getDataStream()
    .map(value -> value * 2)
    .subscribe(
    result -> System.out.println(result)
    );
    Imperative
    List data =
    new DataSource().getData();
    for (Integer value : data) {
    int result = value * 2;
    System.out.println(result);
    }
    Output: 8, 4, 2, 14, 12, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  124. ... to map our data
    Reactive
    new DataSource().getDataStream()
    .map(value -> value * 2)
    .subscribe(
    result -> System.out.println(result)
    );
    Imperative
    List data =
    new DataSource().getData();
    for (Integer value : data) {
    int result = value * 2;
    System.out.println(result);
    }
    Output: 8, 4, 2, 14, 12, ...
    Data: 4, 2, 1, 7, 6, ...

    View Slide

  125. ... to group our data

    View Slide

  126. ... to group our data
    List data =
    new DataSource().getData();
    Map> result =
    new HashMap<>();
    String evenKey = "EVEN";
    String oddKey = "ODD";
    for (Integer value : data) {
    String key;
    if (value % 2 == 0) {
    key = evenKey;
    } else {
    key = oddKey;
    }
    result.putIfAbsent(
    key,
    new ArrayList<>());
    result.get(key).add(datum);
    }
    System.out.println(result);
    Output: {EVEN=[4, 2, 6], ODD=[1, 7]}
    Data: 4, 2, 1, 7, 6

    View Slide

  127. ... to group our data
    http://reactivex.io/documentation/operators/groupby

    View Slide

  128. ... to group our data
    new DataSource().getDataStream()
    .groupBy(
    value -> (value % 2) == 0 ? "EVEN" : "ODD"
    )
    .collectInto(
    // put our groups into a hashmap
    // this is actually still a little messy
    )
    .subscribe(
    resultMap -> System.out.println(resultMap)
    );
    Output: {EVEN=[4, 2, 6], ODD=[1, 7]}
    Data: 4, 2, 1, 7, 6

    View Slide

  129. ... to group our data
    new DataSource().getDataStream()
    .groupBy(
    value -> (value % 2) == 0 ? "EVEN" : "ODD"
    )
    .collectInto(
    // put our groups into a hashmap
    // this is actually still a little messy
    )
    .subscribe(
    resultMap -> System.out.println(resultMap)
    );
    List data =
    new DataSource().getData();
    Map> result =
    new HashMap<>();
    String evenKey = "EVEN";
    String oddKey = "ODD";
    for (Integer value : data) {
    String key;
    if (value % 2 == 0) {
    key = evenKey;
    } else {
    key = oddKey;
    }
    result.putIfAbsent(
    key,
    new ArrayList<>());
    result.get(key).add(datum);
    }
    System.out.println(result);
    Output: {EVEN=[4, 2, 6], ODD=[1, 7]}
    Data: 4, 2, 1, 7, 6

    View Slide

  130. So many more operators
    ! Transforming
    ! Buffer
    ! Scan
    ! Filtering
    ! Debounce
    ! Distinct
    ! First
    ! Last
    ! Take
    ! Min
    ! Combining
    ! Join
    ! Merge
    ! Zip
    ! Math
    ! Average
    ! Concat
    ! Max

    View Slide

  131. Some operations can get complicated
    http://reactivex.io/documentation/operators/join

    View Slide

  132. introduction
    background
    examples
    live demo
    wrap-up

    View Slide

  133. introduction
    background
    examples
    live demo
    wrap-up

    View Slide

  134. Reactive
    Programming
    1. Data streams

    2. Functional Programming

    3. Asynchronous observers

    View Slide

  135. Thanks!

    View Slide