forked from jhusain/learnrxjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObservableExercises.java
More file actions
126 lines (111 loc) · 4.17 KB
/
ObservableExercises.java
File metadata and controls
126 lines (111 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package learnrxjava;
import learnrxjava.types.JSON;
import learnrxjava.types.Movies;
import rx.Observable;
public class ObservableExercises {
/**
* Return an Observable that emits a single value "Hello World!"
*
* @return "Hello World!"
*/
public Observable<String> exerciseHello() {
return Observable.error(new RuntimeException("Not Implemented"));
}
/**
* Transform the incoming Observable from "Hello" to "Hello [Name]" where [Name] is your name.
*
* @param "Hello Name!"
*/
public Observable<String> exerciseMap(Observable<String> hello) {
return Observable.error(new RuntimeException("Not Implemented"));
}
/**
* Given a stream of numbers, choose the even ones and return a stream like:
* <p>
* 2-Even
* 4-Even
* 6-Even
*/
public Observable<String> exerciseFilterMap(Observable<Integer> nums) {
return Observable.error(new RuntimeException("Not Implemented"));
}
/**
* Flatten out all video in the stream of Movies into a stream of videoIDs
*
* @param movieLists
* @return Observable of Integers of Movies.videos.id
*/
public Observable<Integer> exerciseConcatMap(Observable<Movies> movies) {
return Observable.error(new RuntimeException("Not Implemented"));
}
/**
* Flatten out all video in the stream of Movies into a stream of videoIDs
*
* Use flatMap this time instead of concatMap. In Observable streams
* it is almost always flatMap that is wanted, not concatMap as flatMap
* uses merge instead of concat and allows multiple concurrent streams
* whereas concat only does one at a time.
*
* We'll see more about this later when we add concurrency.
*
* @param movieLists
* @return Observable of Integers of Movies.videos.id
*/
public Observable<Integer> exerciseFlatMap(Observable<Movies> movies) {
return Observable.error(new RuntimeException("Not Implemented"));
}
/**
* Retrieve the largest number.
*
* Use reduce to select the maximum value in a list of numbers.
*/
public Observable<Integer> exerciseReduce(Observable<Integer> nums) {
return Observable.error(new RuntimeException("Not Implemented"));
}
/**
* Retrieve the id, title, and <b>smallest</b> box art url for every video.
*
* Now let's try combining reduce() with our other functions to build more complex queries.
*
* This is a variation of the problem we solved earlier, where we retrieved the url of the boxart with a
* width of 150px. This time we'll use reduce() instead of filter() to retrieve the _smallest_ box art in
* the boxarts list.
*
* See Exercise 19 of ComposableListExercises
*/
public Observable<JSON> exerciseMovie(Observable<Movies> movies) {
return Observable.error(new RuntimeException("Not Implemented"));
}
/**
* Combine 2 streams into pairs using zip.
*
* a -> "one", "two", "red", "blue"
* b -> "fish", "fish", "fish", "fish"
* output -> "one fish", "two fish", "red fish", "blue fish"
*/
public Observable<String> exerciseZip(Observable<String> a, Observable<String> b) {
return Observable.error(new RuntimeException("Not Implemented"));
}
/**
* Don't modify any values in the stream but do handle the error
* and replace it with "default-value".
*/
public Observable<String> handleError(Observable<String> data) {
return Observable.error(new RuntimeException("Not Implemented"));
}
/**
* The data stream fails intermittently so return the stream
* with retry capability.
*/
public Observable<String> retry(Observable<String> data) {
return Observable.error(new RuntimeException("Not Implemented"));
}
// This function can be used to build JSON objects within an expression
private static JSON json(Object... keyOrValue) {
JSON json = new JSON();
for (int counter = 0; counter < keyOrValue.length; counter += 2) {
json.put((String) keyOrValue[counter], keyOrValue[counter + 1]);
}
return json;
}
}