Skip to content

Commit 4840748

Browse files
committed
2.2 Reworked chapter
1 parent 5f4acdd commit 4840748

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

Part 2 - Sequence Basics/2. Reducing a sequence.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Reducing a sequence
22

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.
44

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.
66

77
### Marble diagrams
88

@@ -12,15 +12,15 @@ This is an appropriate time to introduce to concept of marble diagrams. It is a
1212

1313
## Filter
1414

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.
1616

1717
```java
1818
public final Observable<T> filter(Func1<? super T,java.lang.Boolean> predicate)
1919
```
2020

2121
![](https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/filter.png)
2222

23-
Now we will create a sequence of numbers and filter out all the even ones, keeping the odd.
23+
We will use `filter` to create a sequence of numbers and filter out all the even ones, keeping only odd values.
2424

2525
```java
2626
Observable<Integer> values = Observable.range(0,10);
@@ -107,7 +107,7 @@ Subscription subscription = values
107107

108108
"Fourth" and "Fifth" were filtered out because their is is 'F' and that has already appeared in "First".
109109

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.
111111

112112
A variant of `distinct` is `distinctUntilChanged`. The difference is that only consecutive non-distinct values are filtered out.
113113

@@ -201,7 +201,7 @@ Completed
201201

202202
## skip and take
203203

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.
205205

206206
```java
207207
Observable<T> take(int num)
@@ -228,9 +228,9 @@ Output
228228
Completed
229229
```
230230

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.
232232

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.
234234

235235
```java
236236
Observable<Integer> values = Observable.create(o -> {
@@ -253,7 +253,7 @@ Output
253253
Completed
254254
```
255255

256-
`skip` is the other half of `take`.
256+
`skip` returns the other half of a `take`.
257257

258258
```java
259259
Observable<T> skip(int num)
@@ -334,7 +334,7 @@ Output
334334
Completed
335335
```
336336

337-
As you would expect, `skipWhile` return the other half of the observable
337+
As you would expect, `skipWhile` returns the other half of the sequence
338338

339339
```java
340340
Observable<Long> values = Observable.interval(100, TimeUnit.MILLISECONDS);
@@ -358,7 +358,7 @@ Output
358358

359359
## skipLast and takeLast
360360

361-
`skipLast` and `takeLast` work just like `take` and `skip`, with the difference that the cutoff points are measured from the end.
361+
`skipLast` and `takeLast` work just like `take` and `skip`, with the difference that the point of reference is from the end.
362362

363363
```java
364364
Observable<Integer> values = Observable.range(0,5);
@@ -379,14 +379,14 @@ Subscription subscription = values
379379
Completed
380380
```
381381

382-
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.
383383

384384

385385
## takeUntil and skipUntil
386386

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`.
388388

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.
390390

391391
```java
392392
public final <E> Observable<T> takeUntil(Observable<? extends E> other)
@@ -414,9 +414,9 @@ Output
414414
Completed
415415
```
416416

417-
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.
418418

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.
420420

421421
```java
422422
Observable<Long> values = Observable.interval(100,TimeUnit.MILLISECONDS);

0 commit comments

Comments
 (0)