Skip to content

Commit bc40695

Browse files
hoangnam2261akarnokd
authored andcommitted
#6323 Java 8 version for Problem-Solving-Examples-in-RxJava (#6324)
* #6323 Java 8 version for Project Euler problem * #6323 Java 8 version for Generate the Fibonacci Sequence * #6323 Java 8 version for Project Euler problem - Fix name of variable * #6324 Fixing for code review.
1 parent 6ae765a commit bc40695

File tree

1 file changed

+56
-6
lines changed

1 file changed

+56
-6
lines changed

Diff for: docs/Problem-Solving-Examples-in-RxJava.md

+56-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This page will present some elementary RxJava puzzles and walk through some solutions (using the Groovy language implementation of RxJava) as a way of introducing you to some of the RxJava operators.
1+
This page will present some elementary RxJava puzzles and walk through some solutions as a way of introducing you to some of the RxJava operators.
22

33
# Project Euler problem #1
44

@@ -7,10 +7,22 @@ There used to be a site called "Project Euler" that presented a series of mathem
77
> If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
88
99
There are several ways we could go about this with RxJava. We might, for instance, begin by going through all of the natural numbers below 1000 with [`range`](Creating-Observables#range) and then [`filter`](Filtering-Observables#filter) out those that are not a multiple either of 3 or of 5:
10+
### Java
11+
```java
12+
Observable<Integer> threesAndFives = Observable.range(1, 999).filter(e -> e % 3 == 0 || e % 5 == 0);
13+
```
14+
### Groovy
1015
````groovy
1116
def threesAndFives = Observable.range(1,999).filter({ !((it % 3) && (it % 5)) });
1217
````
1318
Or, we could generate two Observable sequences, one containing the multiples of three and the other containing the multiples of five (by [`map`](https://github.com/Netflix/RxJava/wiki/Transforming-Observables#map)ping each value onto its appropriate multiple), making sure to only generating new multiples while they are less than 1000 (the [`takeWhile`](Conditional-and-Boolean-Operators#takewhile-and-takewhilewithindex) operator will help here), and then [`merge`](Combining-Observables#merge) these sets:
19+
### Java
20+
```java
21+
Observable<Integer> threes = Observable.range(1, 999).map(e -> e * 3).takeWhile(e -> e < 1000);
22+
Observable<Integer> fives = Observable.range(1, 999).map(e -> e * 5).takeWhile(e -> e < 1000);
23+
Observable<Integer> threesAndFives = Observable.merge(threes, fives).distinct();
24+
```
25+
### Groovy
1426
````groovy
1527
def threes = Observable.range(1,999).map({it*3}).takeWhile({it<1000});
1628
def fives = Observable.range(1,999).map({it*5}).takeWhile({it<1000});
@@ -21,6 +33,11 @@ Don't forget the [`distinct`](Filtering-Observables#distinct) operator here, oth
2133
Next, we want to sum up the numbers in the resulting sequence. If you have installed the optional `rxjava-math` module, this is elementary: just use an operator like [`sumInteger` or `sumLong`](Mathematical-and-Aggregate-Operators#suminteger-sumlong-sumfloat-and-sumdouble) on the `threesAndFives` Observable. But what if you don't have this module? How could you use standard RxJava operators to sum up a sequence and emit that sum?
2234

2335
There are a number of operators that reduce a sequence emitted by a source Observable to a single value emitted by the resulting Observable. Most of the ones that are not in the `rxjava-math` module emit boolean evaluations of the sequence; we want something that can emit a number. The [`reduce`](Mathematical-and-Aggregate-Operators#reduce) operator will do the job:
36+
### Java
37+
```java
38+
Single<Integer> summer = threesAndFives.reduce(0, (a, b) -> a + b);
39+
```
40+
### Groovy
2441
````groovy
2542
def summer = threesAndFives.reduce(0, { a, b -> a+b });
2643
````
@@ -38,6 +55,12 @@ Here is how `reduce` gets the job done. It starts with 0 as a seed. Then, with e
3855
</tbody>
3956
</table>
4057
Finally, we want to see the result. This means we must [subscribe](Observable#onnext-oncompleted-and-onerror) to the Observable we have constructed:
58+
59+
### Java
60+
```java
61+
summer.subscribe(System.out::print);
62+
```
63+
### Groovy
4164
````groovy
4265
summer.subscribe({println(it);});
4366
````
@@ -47,11 +70,24 @@ summer.subscribe({println(it);});
4770
How could you create an Observable that emits [the Fibonacci sequence](http://en.wikipedia.org/wiki/Fibonacci_number)?
4871

4972
The most direct way would be to use the [`create`](Creating-Observables#wiki-create) operator to make an Observable "from scratch," and then use a traditional loop within the closure you pass to that operator to generate the sequence. Something like this:
73+
### Java
74+
```java
75+
Observable<Integer> fibonacci = Observable.create(emitter -> {
76+
int f1 = 0, f2 = 1, f = 1;
77+
while (!emitter.isDisposed()) {
78+
emitter.onNext(f);
79+
f = f1 + f2;
80+
f1 = f2;
81+
f2 = f;
82+
}
83+
});
84+
```
85+
### Groovy
5086
````groovy
51-
def fibonacci = Observable.create({ observer ->
52-
def f1=0; f2=1, f=1;
53-
while(!observer.isUnsubscribed() {
54-
observer.onNext(f);
87+
def fibonacci = Observable.create({ emitter ->
88+
def f1=0, f2=1, f=1;
89+
while(!emitter.isDisposed()) {
90+
emitter.onNext(f);
5591
f = f1+f2;
5692
f1 = f2;
5793
f2 = f;
@@ -61,7 +97,16 @@ def fibonacci = Observable.create({ observer ->
6197
But this is a little too much like ordinary linear programming. Is there some way we can instead create this sequence by composing together existing Observable operators?
6298

6399
Here's an option that does this:
64-
````
100+
### Java
101+
```java
102+
Observable<Integer> fibonacci =
103+
Observable.fromArray(0)
104+
.repeat()
105+
.scan(new int[]{0, 1}, (a, b) -> new int[]{a[1], a[0] + a[1]})
106+
.map(a -> a[1]);
107+
```
108+
### Groovy
109+
````groovy
65110
def fibonacci = Observable.from(0).repeat().scan([0,1], { a,b -> [a[1], a[0]+a[1]] }).map({it[1]});
66111
````
67112
It's a little [janky](http://www.urbandictionary.com/define.php?term=janky). Let's walk through it:
@@ -73,6 +118,11 @@ This has the effect of emitting the following sequence of items: `[0,1], [1,1],
73118
The second item in this array describes the Fibonacci sequence. We can use `map` to reduce the sequence to just that item.
74119

75120
To print out a portion of this sequence (using either method), you would use code like the following:
121+
### Java
122+
```java
123+
fibonacci.take(15).subscribe(System.out::println);
124+
```
125+
### Groovy
76126
````groovy
77127
fibonnaci.take(15).subscribe({println(it)})];
78128
````

0 commit comments

Comments
 (0)