You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#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.
Copy file name to clipboardExpand all lines: docs/Problem-Solving-Examples-in-RxJava.md
+56-6
Original file line number
Diff line number
Diff 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.
2
2
3
3
# Project Euler problem #1
4
4
@@ -7,10 +7,22 @@ There used to be a site called "Project Euler" that presented a series of mathem
7
7
> 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.
8
8
9
9
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);
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);
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?
22
34
23
35
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
24
41
````groovy
25
42
def summer = threesAndFives.reduce(0, { a, b -> a+b });
26
43
````
@@ -38,6 +55,12 @@ Here is how `reduce` gets the job done. It starts with 0 as a seed. Then, with e
38
55
</tbody>
39
56
</table>
40
57
Finally, we want to see the result. This means we must [subscribe](Observable#onnext-oncompleted-and-onerror) to the Observable we have constructed:
How could you create an Observable that emits [the Fibonacci sequence](http://en.wikipedia.org/wiki/Fibonacci_number)?
48
71
49
72
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:
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?
62
98
63
99
Here's an option that does this:
64
-
````
100
+
### Java
101
+
```java
102
+
Observable<Integer> fibonacci =
103
+
Observable.fromArray(0)
104
+
.repeat()
105
+
.scan(newint[]{0, 1}, (a, b) ->newint[]{a[1], a[0] + a[1]})
0 commit comments