Skip to content

Commit 9e5cb08

Browse files
committed
2.4 Added nest
1 parent c21bcaf commit 9e5cb08

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

Part 2 - Sequence Basics/4. Aggregation.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ Nested observables may be confusing at first, but they are a powerful construct
519519

520520

521521
* Partitions of Data
522-
* You may partition data from a single source so that it can easily be filtered and shared to many sources. Partitioning data may also be useful for aggregates as we have seen. This is commonly done with the GroupBy operator.
522+
* You may partition data from a single source so that it can easily be filtered and shared to many sources. Partitioning data may also be useful for aggregates as we have seen. This is commonly done with the `groupBy` operator.
523523
* Online Game servers
524524
* Consider a sequence of servers. New values represent a server coming online. The value itself is a sequence of latency values allowing the consumer to see real time information of quantity and quality of servers available. If a server went down then the inner sequence can signify that by completing.
525525
* Financial data streams
@@ -528,6 +528,27 @@ Nested observables may be confusing at first, but they are a powerful construct
528528
* Users can join a chat (outer sequence), leave messages (inner sequence) and leave a chat (completing the inner sequence).
529529
* File watcher
530530
* As files are added to a directory they could be watched for modifications (outer sequence). The inner sequence could represent changes to the file, and completing an inner sequence could represent deleting the file.
531+
532+
### nest
533+
534+
When dealing with nested observables, the `nest` operator becomes useful. It allows you to turn a non-nested observable into a nested observable. `nest` takes a source observable and returns an observable that will the source observable and then terminate.
535+
536+
![](https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/nest.png)
537+
538+
```java
539+
Observable.range(0, 3)
540+
.nest()
541+
.subscribe(ob -> ob.subscribe(System.out::println));
542+
```
543+
Output
544+
```
545+
0
546+
1
547+
2
548+
```
549+
550+
Nesting observables to consume them doesn't make much sense. Towards the end of the pipeline, you'd rather flatten and simply your observables. Nesting is useful when you need to make a non-nested observable be of the same type as a nested observable that you have from elsewhere. Once they are of the same type, you can for example combine them, as we will see in the chapter about [combining sequences](/Part 3 - Taming the sequence/4. Combining sequences.md).
551+
531552

532553

533554
#### Continue reading

0 commit comments

Comments
 (0)