Skip to content

Commit ef7d4d4

Browse files
committed
2.3 Reworked chapter
1 parent 4840748 commit ef7d4d4

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

Part 2 - Sequence Basics/3. Inspection.md

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Inspection
22

3-
We've just finished seeing ways to filter out data that we don't care about. Sometimes what we want is information about the sequence rather than its values. We will now introduce a few methods that revolve around reasoning about the sequence.
3+
In the previous chapter we just saw ways to filter out data that we don't care about. Sometimes what we want is information about the sequence rather than the values themselves. We will now introduce some methods that allow us to reason about a sequence.
44

55
## all
66

7-
The `all` methods establishes that every value emitted by an observable meets a criterion. Here's the signature and an example:
7+
The `all` method establishes that every value emitted by an observable meets a criterion. Here's the signature and an example:
88

99
```java
1010
public final Observable<java.lang.Boolean> all(Func1<? super T,java.lang.Boolean> predicate)
@@ -35,13 +35,13 @@ true
3535
Completed
3636
```
3737

38-
An interesting fact about this method is that it returns an observable with a single value, rather than the boolean value directly. This is because it is unknown how long it will take to establish whether the result should be true or false. Another interesting fact is that it returns its result and completes as soon as it can know. As soon as an item fails the predicate, `false` will be emitted. A value of `true` on the other hand cannot be emitted until the source sequence has completed. We can see that in the next example
38+
An interesting fact about this method is that it returns an observable with a single value, rather than the boolean value directly. This is because it is unknown how long it will take to establish whether the result should be true or false. Even though it completes as soon as it can know, that may take as long the source sequence itself. As soon as an item fails the predicate, `false` will be emitted. A value of `true` on the other hand cannot be emitted until the source sequence has completed and _all_ of the items are checked. Returning the decision inside an observable is a convenient way of making the operation non-blocking. We can see `all` failing as soon as possible in the next example:
3939

4040
```java
4141
Observable<Long> values = Observable.interval(150, TimeUnit.MILLISECONDS).take(5);
4242

4343
Subscription subscription = values
44-
.all(i -> i<3)
44+
.all(i -> i<3) // Will fail eventually
4545
.subscribe(
4646
v -> System.out.println("All: " + v),
4747
e -> System.out.println("All: Error: " + e),
@@ -67,8 +67,6 @@ All: Completed
6767
Completed
6868
```
6969

70-
There is an Rx operator that appears in other implementations but not in RxJava, called `any`. You can easily get the same behaviour by negating the predicate of the `all` operator.
71-
7270
If the source observable emits an error, then `all` becomes irrelavant and the error passes through, terminating the sequence.
7371

7472
```java
@@ -92,7 +90,7 @@ Output
9290
Error: java.lang.Exception
9391
```
9492

95-
If, however, the predicate fails, then `false` is emitted and the sequence terminates. Even if the source observable fails after that, the event is ignored. Remember that it is Rx's contract that nothing is emitted after a termination of an observable.
93+
If, however, the predicate fails, then `false` is emitted and the sequence terminates. Even if the source observable fails after that, the event is ignored, as required by the Rx contract (no events after a termination event).
9694

9795
```java
9896
Observable<Integer> values = Observable.create(o -> {
@@ -174,11 +172,11 @@ false
174172
Completed
175173
```
176174

177-
Once again, falsehood is established as soon as the first value is emitted. True will only be returned once the source observable has terminated.
175+
Falsehood is established as soon as the first value is emitted. True will can be returned once the source observable has terminated.
178176

179177
## contains
180178

181-
The method `contains` establishes if a particular element is emitter by an observable. `contains` will use the `Object.equals` function to establish the equality. Just like previous operators, it emits its decision as soon as it can be established and immediately completes.
179+
The method `contains` establishes if a particular element is emitted by an observable. `contains` will use the `Object.equals` method to establish the equality. Just like previous operators, it emits its decision as soon as it can be established and immediately completes.
182180

183181
![](https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/contains.png)
184182

@@ -200,7 +198,7 @@ true
200198
Completed
201199
```
202200

203-
If we had used `contains(4)` where we used `contains(4L)`, nothing would be printed. That's because `4` and `4L` are not equal in Java. Our code would wait for the observable to complete to return false, only the observable we used is infinite.
201+
If we had used `contains(4)` where we used `contains(4L)`, nothing would be printed. That's because `4` and `4L` are not equal in Java. Our code would wait for the observable to complete before returning false, but the observable we used is infinite.
204202

205203
## defaultIfEmpty
206204

@@ -209,10 +207,9 @@ If an empty sequence would cause you problems, rather than checking with `isEmpt
209207
![](https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/defaultIfEmpty.png)
210208

211209
```java
212-
Observable<Integer> values = Observable.range(0,10);
210+
Observable<Integer> values = Observable.empty();
213211

214212
Subscription subscription = values
215-
.take(0)
216213
.defaultIfEmpty(2)
217214
.subscribe(
218215
v -> System.out.println(v),
@@ -227,9 +224,7 @@ Output
227224
Completed
228225
```
229226

230-
Here we emptied an observable with `take(0)`. We could have used `Observable.empty`.
231-
232-
The value is emitted only if no other values appeared and only on successful completion. If the source is not empty, the result is just the source observable. In the case of the error, the default value will _not_ be emitted before the error.
227+
The default value is emitted only if no other values appeared and only on successful completion. If the source is not empty, the result is just the source observable. In the case of the error, the default value will _not_ be emitted before the error.
233228

234229
```java
235230
Observable<Integer> values = Observable.error(new Exception());
@@ -250,7 +245,7 @@ Error: java.lang.Exception
250245

251246
## elementAt
252247

253-
You can select exactly one element our of an observable using the `elementAt` method
248+
You can select exactly one element out of an observable using the `elementAt` method
254249

255250
![](https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/elementAt.png)
256251

@@ -294,7 +289,7 @@ Completed
294289

295290
## sequenceEqual
296291

297-
The last method establishes that two sequences are equal by comparing the value at the same indices. Both the size of the sequences and the values must be equal. The function will either use `Object.equals` or the function that you supply to compare values.
292+
The last operator for this chapter establishes that two sequences are equal by comparing the values at the same index. Both the size of the sequences and the values must be equal. The function will either use `Object.equals` or the function that you supply to compare values.
298293

299294
```java
300295
Observable<String> strings = Observable.just("1", "2", "3");
@@ -315,7 +310,7 @@ true
315310
Completed
316311
```
317312

318-
If we had used the overload that was commented out (i.e the standard `Object.equals`), the result would be `false`.
313+
If swap the operator for the one that is commented out (i.e the one that uses the the standard `Object.equals`), the result would be `false`.
319314

320315
Failing is not part of the comparison. As soon as either sequence fails, the resulting observable forwards the error.
321316

0 commit comments

Comments
 (0)