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
Copy file name to clipboardExpand all lines: Part 2 - Sequence Basics/3. Inspection.md
+13-18Lines changed: 13 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
# Inspection
2
2
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.
4
4
5
5
## all
6
6
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:
8
8
9
9
```java
10
10
publicfinalObservable<java.lang.Boolean> all(Func1<? super T,java.lang.Boolean> predicate)
@@ -35,13 +35,13 @@ true
35
35
Completed
36
36
```
37
37
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:
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
-
72
70
If the source observable emits an error, then `all` becomes irrelavant and the error passes through, terminating the sequence.
73
71
74
72
```java
@@ -92,7 +90,7 @@ Output
92
90
Error: java.lang.Exception
93
91
```
94
92
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).
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.
178
176
179
177
## contains
180
178
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.
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.
204
202
205
203
## defaultIfEmpty
206
204
@@ -209,10 +207,9 @@ If an empty sequence would cause you problems, rather than checking with `isEmpt
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.
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.
0 commit comments