-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
ObservableScan.java
66 lines (54 loc) · 1.74 KB
/
ObservableScan.java
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
package rx.observables.transforming;
import org.junit.Test;
import rx.Observable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author Pablo Perez
* Scan it just works as redude on Java 8 Stream, pass into the function the last emitted item and the new one.
*/
public class ObservableScan {
/**
* apply this function for every item against the previous emitted item from the source.
* Emitted:
* 0
* 1
* 3
* 6
* 10
* 15
*/
@Test
public void scanObservable() {
Integer[] numbers = {0, 1, 2, 3, 4, 5};
Observable.from(numbers)
.scan((lastItemEmitted, newItem) -> lastItemEmitted + newItem)
.subscribe(System.out::println);
}
@Test
public void scanObservableIntoList() {
Integer[] numbers = {0, 1, 2, 3, 4, 5};
Observable.from(numbers)
.scan(new ArrayList<>(),
(lastItemEmitted, newItem) -> {
lastItemEmitted.add(newItem);
return lastItemEmitted;
})
.subscribe(System.out::println);
}
@Test
public void scanObservableList() {
Observable.from(Arrays.asList(1, 2))
.flatMap(item -> getFirstList())
.scan((lastList, newList) ->
Stream.concat(lastList.stream(),
newList.stream()).collect(Collectors.toList()))
.subscribe(System.out::println);
}
private Observable<List<Integer>> getFirstList() {
return Observable.just(Arrays.asList(1, 2, 3, 4));
}
}