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/2. Reducing a sequence.md
+16-16Lines changed: 16 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
# Reducing a sequence
2
2
3
-
The examples we've seen so far where all very small. Nothing should stop you from using Rx on a huge stream of realtime data, but what good would Rx be if it dumped the whole data onto you, and let you handle it like you would otherwise? Here we will explore operators that will filter out irrelevant data, or reduce the data to the single value that you want.
3
+
The examples we've seen so far were all very small. Nothing should stop you from using Rx on a huge stream of realtime data, but what good would Rx be if it dumped the whole bulk of the data onto you, and force you handle it like you would otherwise? Here we will explore operators that can filter out irrelevant data, or reduce the data to the single value that you want.
4
4
5
-
As usual, most of the operators here will be familiar to anyone that has worked with Java's `Stream`s, or functional programming in general. All the operators here return a new observable and do _not_ affect the original observable. This principle is present throughout Rx. Transformations of observables create a new observable every time and leave the original unaffected. Subscribers to the original observable should notice no change, but we will see in later chapters that guaranteeing this may require caution from the developer as well.
5
+
Most of the operators here will be familiar to anyone who has worked with Java's `Stream`s or functional programming in general. All the operators here return a new observable and do _not_ affect the original observable. This principle is present throughout Rx. Transformations of observables create a new observable every time and leave the original unaffected. Subscribers to the original observable should notice no change, but we will see in later chapters that guaranteeing this may require caution from the developer as well.
6
6
7
7
### Marble diagrams
8
8
@@ -12,15 +12,15 @@ This is an appropriate time to introduce to concept of marble diagrams. It is a
12
12
13
13
## Filter
14
14
15
-
`filter` takes a predicate function that takes an emitted value and returns a boolean decision on if it should be kept.
15
+
`filter` takes a predicate function that makes a boolean decision for each value emitted. If the decision is `false`, the item is omitted from the filtered sequence.
16
16
17
17
```java
18
18
publicfinalObservable<T> filter(Func1<? super T,java.lang.Boolean> predicate)
"Fourth" and "Fifth" were filtered out because their is is 'F' and that has already appeared in "First".
109
109
110
-
An experienced programmed already knows that this operator houses a set that keeps every unique value that passes through the observable and checks every new value against it. While Rx operators neatly hide these things, you should still be aware the an Rx operator can have significant cost and consider what kind of an observable it is used on.
110
+
An experienced programmer already knows that this operator maintains a set internally with every unique value that passes through the observable and checks every new value against it. While Rx operators neatly hide these things, you should still be aware that an Rx operator can have a significant cost and consider what you are using it on.
111
111
112
112
A variant of `distinct` is `distinctUntilChanged`. The difference is that only consecutive non-distinct values are filtered out.
113
113
@@ -201,7 +201,7 @@ Completed
201
201
202
202
## skip and take
203
203
204
-
The next group of methods serve to cut the sequence at an indexed point, and either take the first half or take the second. `take` takes the first n elements and`skip` skips them. Note that neither function considers it an error if there are too few items in the sequence.
204
+
The next group of methods serve to cut the sequence at a specific point based on the item's index, and either take the first part or the second part. `take` takes the first n elements, while`skip` skips them. Note that neither function considers it an error if there are fewer items in the sequence than the specified index.
205
205
206
206
```java
207
207
Observable<T> take(int num)
@@ -228,9 +228,9 @@ Output
228
228
Completed
229
229
```
230
230
231
-
Users of Java 8 streams will be know the `take` operator as `limit`. The `limit` operator exists in Rx too, for symmetry purposes. It is an alias of `take`, but it lacks the richer overloads.
231
+
Users of Java 8 streams will be know the `take` operator as `limit`. The `limit` operator exists in Rx too, for symmetry purposes. It is an alias of `take`, but it lacks the richer overloads that we will soon see.
232
232
233
-
`take` completes as soon as the n-th item is available and does not care what the source emits after that.
233
+
`take` completes as soon as the n-th item is available. If an error occurs, the error will be forwarded, but not if it occurs after the cutting point. `take` doesn't care what happens in the observable after the n-th item.
By now you are in position to understand how `takeLast` is related to `skipLast`. There are overloads for both indices and time.
382
+
By now you should be able to guess how `takeLast` is related to `skipLast`. There are overloads for both indices and time.
383
383
384
384
385
385
## takeUntil and skipUntil
386
386
387
-
There are also methods `takeUntil` and `skipUntil`. `takeUntil` works exactly like `takeWhile` except that it takes items while the predictate is false. The same is true of `skipUntil`.
387
+
There are also two methods named `takeUntil` and `skipUntil`. `takeUntil` works exactly like `takeWhile` except that it takes items while the predictate is false. The same is true of `skipUntil`.
388
388
389
-
Along with that, `takeUntil` and `skipUntil` have a very interesting overload. The cutoff point is defined as the moment when _another_ observable emits an item. Note that this can be any item as the actual value is dismissed.
389
+
Along with that, `takeUntil` and `skipUntil`each have a very interesting overload. The cutoff point is defined as the moment when _another_ observable emits an item.
As you remember, `timer` here will wait 250ms and emit one event. This signals the `takeUntil` to stop.
417
+
As you may remember, `timer` here will wait 250ms and emit one event. This signals `takeUntil` to stop the sequence. Note that the signal can be of any type, since the actual value is not used.
418
418
419
-
Once again `skipUntil` works by the same rules and returns the other half of the observable.
419
+
Once again `skipUntil` works by the same rules and returns the other half of the observable. Values are ignored until the signal comes to start letting values through.
0 commit comments