Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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


Slide 3

Slide 3 text

Reactive Programming Allows us to manipulate a stream of data with functions.
 (asynchronously)

Slide 4

Slide 4 text

Reactive Programming

Slide 5

Slide 5 text

Reactive Programming 1. Data streams


Slide 6

Slide 6 text

Reactive Programming 1. Data streams
 2. Functional Programming


Slide 7

Slide 7 text

Reactive Programming 1. Data streams
 2. Functional Programming
 3. Asynchronous observers

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Who actually uses this?

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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)

Slide 17

Slide 17 text

Reactive Programming Theory

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

4 2 1 7 6

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

4 2 1 7 6 filter / map / count / 
 max / min / delay / ... SUBJECT OBSERVER

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

some function that takes ⏳ SUBJECT OBSERVER

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

some function that takes ⏳ SUBJECT OBSERVER another function

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

What creates the subject data stream?

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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)

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

introduction background examples live demo wrap-up

Slide 47

Slide 47 text

What languages are you familiar with?

Slide 48

Slide 48 text

~80 % Percent of the most popular languages are imperative 
 or multi-paradigmatic languages Source: StackOverflow Developer Survey 2018

Slide 49

Slide 49 text

What's a language paradigm?

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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.

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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)

Slide 58

Slide 58 text

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)

Slide 59

Slide 59 text

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)

Slide 60

Slide 60 text

Imperative vs. Functional

Slide 61

Slide 61 text

Imperative vs. Functional Imperative Programming

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

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 }

Slide 68

Slide 68 text

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 }

Slide 69

Slide 69 text

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 }

Slide 70

Slide 70 text

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 }

Slide 71

Slide 71 text

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 }

Slide 72

Slide 72 text

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 }

Slide 73

Slide 73 text

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 }

Slide 74

Slide 74 text

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; }

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

Reactive Programming
 1. Data streams
 
 2. Functional programming
 
 3. Asynchronous observers


Slide 78

Slide 78 text

Reactive Programming
 1. Data streams
 
 2. Functional programming
 
 3. Asynchronous observers


Slide 79

Slide 79 text

A flow of data objects from one function to the next Reactive Programming
 1. Data streams
 
 2. Functional programming
 
 3. Asynchronous observers


Slide 80

Slide 80 text

Reactive Programming
 1. Data streams
 
 2. Functional programming
 
 3. Asynchronous observers


Slide 81

Slide 81 text

! Functions are meant to be pure Reactive Programming
 1. Data streams
 
 2. Functional programming
 
 3. Asynchronous observers


Slide 82

Slide 82 text

! 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


Slide 83

Slide 83 text

! 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


Slide 84

Slide 84 text

! 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


Slide 85

Slide 85 text

Reactive Programming
 1. Data streams
 
 2. Functional programming
 
 3. Asynchronous observers


Slide 86

Slide 86 text

Uses the Observer pattern with Subject data sources that notify Observers Reactive Programming
 1. Data streams
 
 2. Functional programming
 
 3. Asynchronous observers


Slide 87

Slide 87 text

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


Slide 88

Slide 88 text

Reactive Programming is NOT

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

RxJava

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

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

Slide 95

Slide 95 text

introduction background examples live demo wrap-up

Slide 96

Slide 96 text

... 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, ...

Slide 97

Slide 97 text

... 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, ...

Slide 98

Slide 98 text

... 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, ...

Slide 99

Slide 99 text

... 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, ...

Slide 100

Slide 100 text

... 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, ...

Slide 101

Slide 101 text

... 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, ...

Slide 102

Slide 102 text

... 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, ...

Slide 103

Slide 103 text

... 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, ...

Slide 104

Slide 104 text

... 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, ...

Slide 105

Slide 105 text

... 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, ...

Slide 106

Slide 106 text

... 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, ...

Slide 107

Slide 107 text

... to subscribe to data Reactive new DataSource().getDataStream() .subscribe( value -> updateUI(value) ); Imperative List data = 
 new DataSource().getData(); for (Integer value : data) { updateUI(value); }

Slide 108

Slide 108 text

... to subscribe to data Reactive new DataSource().getDataStream() .subscribe( value -> saveToDatabase(value) ); Imperative List data = 
 new DataSource().getData(); for (Integer value : data) { saveToDatabase(value); }

Slide 109

Slide 109 text

... to subscribe to data changes

Slide 110

Slide 110 text

... 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);

Slide 111

Slide 111 text

... 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); ???

Slide 112

Slide 112 text

... 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) );

Slide 113

Slide 113 text

... to filter our data

Slide 114

Slide 114 text

... 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, ...

Slide 115

Slide 115 text

... 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, ...

Slide 116

Slide 116 text

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

Slide 117

Slide 117 text

... 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, ...

Slide 118

Slide 118 text

... 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, ...

Slide 119

Slide 119 text

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

Slide 120

Slide 120 text

… 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, ...

Slide 121

Slide 121 text

... 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, ...

Slide 122

Slide 122 text

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

Slide 123

Slide 123 text

... 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, ...

Slide 124

Slide 124 text

... 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, ...

Slide 125

Slide 125 text

... to group our data

Slide 126

Slide 126 text

... 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

Slide 127

Slide 127 text

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

Slide 128

Slide 128 text

... 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

Slide 129

Slide 129 text

... 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

Slide 130

Slide 130 text

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

Slide 131

Slide 131 text

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

Slide 132

Slide 132 text

introduction background examples live demo wrap-up

Slide 133

Slide 133 text

introduction background examples live demo wrap-up

Slide 134

Slide 134 text

Reactive Programming 1. Data streams
 2. Functional Programming
 3. Asynchronous observers

Slide 135

Slide 135 text

Thanks!