Skip to content
2 changes: 1 addition & 1 deletion Part 1 - Getting Started/1. Why Rx.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# PART 1 - Getting starter
# PART 1 - Getting started

## Why Rx

Expand Down
7 changes: 3 additions & 4 deletions Part 1 - Getting Started/2. Key types.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ interface Observer<T> {

Those three methods are the behaviour that is executed every time the observable pushes a value. The observer will have its `onNext` called zero or more times, optionally followed by an `onCompleted` or an `onError`. No calls happen after a call to `onError` or `onCompleted`.

When developing Rx code, you'll see a lot of `Observable`, but not so much of `Observer`. While it is important to understand the `Observer`, there are shorthands that that remove the need to instantiate it yourself.
When developing Rx code, you'll see a lot of `Observable`, but not so much of `Observer`. While it is important to understand the `Observer`, there are shorthands that remove the need to instantiate it yourself.


## Implementing Observable and Observer
Expand Down Expand Up @@ -132,9 +132,8 @@ Late: 3
Our late subscriber now missed the first value, which fell off the buffer of size 2. Similarily, old values fall off the buffer as time passes, when the subject is created with `createWithTime`

```java
ReplaySubject<Integer> s = ReplaySubject.createWithTime(
150, TimeUnit.MILLISECONDS,
Schedulers.immediate());
ReplaySubject<Integer> s = ReplaySubject.createWithTime(150, TimeUnit.MILLISECONDS,
Schedulers.immediate());
s.onNext(0);
Thread.sleep(100);
s.onNext(1);
Expand Down
2 changes: 1 addition & 1 deletion Part 1 - Getting Started/3. Lifetime management.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Lifetime management

The idea behind Rx that it is unknown *when* a sequence emits values or terminates, but you still have control over when you begin and stop accepting values. Subscriptions may be linked to allocated resources that you will want to release at the end of a sequence. Rx provides control over your subscriptions to enable you to do that.
The idea behind Rx is that it is unknown *when* a sequence emits values or terminates, but you still have control over when you begin and stop accepting values. Subscriptions may be linked to allocated resources that you will want to release at the end of a sequence. Rx provides control over your subscriptions to enable you to do that.

## Subscribing

Expand Down
12 changes: 6 additions & 6 deletions Part 2 - Sequence Basics/1. Creating a sequence.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# PART 2 - Sequence basics

Now that you understand what Rx is in general, it is time to start creating and manipulating sequences. The original implementation of manipulating sequences was based on C#'s LINQ, which in turn was inspired from functional programming. Knowledge about either isn't necessary, but it would make the learning process a lot easier for the reader. Following the original www.introtorx.com, we too will divide operations into themes that generally go from the simpler to the more advanced. Most Rx operators manipulate existing sequences. But first, we will see how to create an `Observable` to begin with.
Now that you understand what Rx is in general, it is time to start creating and manipulating sequences. The original implementation of manipulating sequences was based on C#'s [LINQ](https://en.wikipedia.org/wiki/Language_Integrated_Query), which in turn was inspired from functional programming. Knowledge about either isn't necessary, but it would make the learning process a lot easier for the reader. Following the original www.introtorx.com, we too will divide operations into themes that generally go from the simpler to the more advanced. Most Rx operators manipulate existing sequences. But first, we will see how to create an `Observable` to begin with.

# Creating a sequence

In previous examples we used `Subject`s and manually pushed values into them to create an sequence. We used that sequence to demonstrate some key concepts and the first and most important Rx method, `subscribe`. In most cases, subjects are not the best way to create a new `Observable`. We will now see tidier ways to create observable sequences.
In previous examples we used `Subject`s and manually pushed values into them to create a sequence. We used that sequence to demonstrate some key concepts and the first and most important Rx method, `subscribe`. In most cases, subjects are not the best way to create a new `Observable`. We will now see tidier ways to create observable sequences.

## Simple factory methods

Expand Down Expand Up @@ -79,7 +79,7 @@ Error: java.lang.Exception: Oops

### Observable.defer

`defer` doesn't define a new kind of observable, but allows you to declare how a source observable should be created every time a subscriber arrives. Consider how you would create an observable that returns the current time and terminates. You are emitting a single value, so it sounds like a case for `just`.
`defer` doesn't define a new kind of observable, but allows you to declare how an observable should be created every time a subscriber arrives. Consider how you would create an observable that returns the current time and terminates. You are emitting a single value, so it sounds like a case for `just`.

```java
Observable<Long> now = Observable.just(System.currentTimeMillis());
Expand Down Expand Up @@ -118,7 +118,7 @@ now.subscribe(System.out::println);
static <T> Observable<T> create(Observable.OnSubscribe<T> f)
```

The `Observable.OnSubscribe<T>` is simpler than it looks. It is basically a function that takes an `Subscriber<T>` for type `T`. Inside it we can manually determine the events that are pushed to the subscriber.
The `Observable.OnSubscribe<T>` is simpler than it looks. It is basically a function that takes a `Subscriber<T>` for type `T`. Inside it we can manually determine the events that are pushed to the subscriber.

```java
Observable<String> values = Observable.create(o -> {
Expand Down Expand Up @@ -228,7 +228,7 @@ The example above waits 2 seconds, then starts counting every 1 second.

There are well established tools for dealing with sequences, collections and asychronous events, which may not be directly compatible with Rx. Here we will discuss ways to turn their output into input for your Rx code.

If you are using an asynchonous tool that uses event handlers, like JavaFX, you can use `Observable.create` to turn the streams into an observable
If you are using an asynchronous tool that uses event handlers, like JavaFX, you can use `Observable.create` to turn the streams into an observable

```java
Observable<ActionEvent> events = Observable.create(o -> {
Expand Down Expand Up @@ -296,7 +296,7 @@ Received: 3
Completed
```

`Observable` is not interchangeable with `Iterable` or `Stream`. `Observable`s are push-based. i.e. the call to `onNext` causes the stack of handlers to execute all the way to the final subscriber method (unless specified otherwise). The other model are pull-based, which means that values are requested as soon as possible and execution blocks until the result is returned.
`Observable` is not interchangeable with `Iterable` or `Stream`. `Observable`s are push-based, i.e., the call to `onNext` causes the stack of handlers to execute all the way to the final subscriber method (unless specified otherwise). The other models are pull-based, which means that values are requested as soon as possible and execution blocks until the result is returned.

#### Continue reading

Expand Down
12 changes: 6 additions & 6 deletions Part 2 - Sequence Basics/2. Reducing a sequence.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Most of the operators here will be familiar to anyone who has worked with Java's

### Marble diagrams

This is an appropriate time to introduce to concept of marble diagrams. It is a popular way of explaining the operators in Rx, because of their intuitive and graphical nature. They are present a lot in the documetation of RxJava and it only makes sense that we take advantage of their explanatory nature. The format is mostly self-explanatory: time flows left to right, shapes represent values, a slash is a onCompletion, an X is an error. The operator is applied to the top sequence and the result is the sequence below.
This is an appropriate time to introduce to concept of marble diagrams. It is a popular way of explaining the operators in Rx, because of their intuitive and graphical nature. They are present a lot in the documentation of RxJava and it only makes sense that we take advantage of their explanatory nature. The format is mostly self-explanatory: time flows left to right, shapes represent values, a slash is a onCompletion, an X is an error. The operator is applied to the top sequence and the result is the sequence below.

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

Expand Down Expand Up @@ -111,7 +111,7 @@ Third
Completed
```

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

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.

Expand Down Expand Up @@ -182,7 +182,7 @@ Completed

## ignoreElements

`ignoreElements` will ignore every value, but lets though `onCompleted` and `onError`.
`ignoreElements` will ignore every value, but lets pass through `onCompleted` and `onError`.

```java
Observable<Integer> values = Observable.range(0, 10);
Expand Down Expand Up @@ -232,7 +232,7 @@ Subscription first2 = values
Completed
```

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.
Users of Java 8 streams should 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.

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

Expand Down Expand Up @@ -310,7 +310,7 @@ Completed

## skipWhile and takeWhile

`take` and `skip` work with predefined indices. If you want to "discover" the cutoff point as the values come, `takeWhile` and `shipWhile` will use a predicate instead. `takeWhile` takes items while a predicate function returns `true`
`take` and `skip` work with predefined indices. If you want to "discover" the cutoff point as the values come, `takeWhile` and `skipWhile` will use a predicate instead. `takeWhile` takes items while a predicate function returns `true`

```java
Observable<T> takeWhile(Func1<? super T,java.lang.Boolean> predicate)
Expand Down Expand Up @@ -414,7 +414,7 @@ Completed

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.

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.
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 pass through.

```java
Observable<Long> values = Observable.interval(100,TimeUnit.MILLISECONDS);
Expand Down
8 changes: 4 additions & 4 deletions Part 2 - Sequence Basics/3. Inspection.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ All: Completed
Completed
```

If the source observable emits an error, then `all` becomes irrelavant and the error passes through, terminating the sequence.
If the source observable emits an error, then `all` becomes irrelevant and the error passes through, terminating the sequence.

```java
Observable<Integer> values = Observable.create(o -> {
Expand Down Expand Up @@ -172,7 +172,7 @@ false
Completed
```

Falsehood is established as soon as the first value is emitted. True will can be returned once the source observable has terminated.
Falsehood is established as soon as the first value is emitted. `true` will be returned once the source observable has terminated.

## contains

Expand Down Expand Up @@ -202,7 +202,7 @@ If we had used `contains(4)` where we used `contains(4L)`, nothing would be prin

## defaultIfEmpty

If an empty sequence would cause you problems, rather than checking with `isEmpty` and handling the case, you can force an obserable to emit a value on completion if didn't emit anything before completing.
If an empty sequence would cause you problems, rather than checking with `isEmpty` and handling the case, you can force an observable to emit a value on completion if it didn't emit anything before completing.

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

Expand Down Expand Up @@ -310,7 +310,7 @@ true
Completed
```

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`.
If we swap the operator for the one that is commented out, i.e, the one using the standard `Object.equals`, the result would be `false`.

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

Expand Down
12 changes: 6 additions & 6 deletions Part 2 - Sequence Basics/4. Aggregation.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ Single1: 5
Single1: Completed
```

Like in with previous methods, you can have a default value with `singleOrDefault`
Like in the previous methods, you can have a default value with `singleOrDefault`

## Custom aggregators

The methods we saw on this chapter so far don't seem that different from the ones in previous chapters. We will now see two very powerful methods that will greatly expand what we can do with an observable. Many of the methods we've seen so far can be implemented using those.

### reduce

You may have heard of `reduce` from MapReduce. Alternatively, you might have met it under the names "aggregate", "accumulate" or "fold". The general idea is that you produce a single value out of many by combining them two at a time. It its most basic overload, all you need is a function that combines two values into one.
You may have heard of `reduce` from [MapReduce] (https://en.wikipedia.org/wiki/MapReduce). Alternatively, you might have met it under the names "aggregate", "accumulate" or "fold". The general idea is that you produce a single value out of many by combining them two at a time. In its most basic overload, all you need is a function that combines two values into one.

```java
public final Observable<T> reduce(Func2<T,T,T> accumulator)
Expand Down Expand Up @@ -279,7 +279,7 @@ values
[10, 11, 12, 13, 14]
```

Usually, you won't have to collect values manually. Rxjava offers a variety of operators for collecting your sequence into a container. Those aggregators return an observable that will emit the corresponding collection when it is ready, just like what we did here. We will see such aggregators next.
Usually, you won't have to collect values manually. RxJava offers a variety of operators for collecting your sequence into a container. Those aggregators return an observable that will emit the corresponding collection when it is ready, just like what we did here. We will see such aggregators next.

#### toList

Expand Down Expand Up @@ -314,7 +314,7 @@ values
.toSortedList((i1,i2) -> i2 - i1)
.subscribe(v -> System.out.println(v));
```
[Output]((/tests/java/itrx/chapter2/aggregation/ToCollectionExample.java)
[Output](/tests/java/itrx/chapter2/aggregation/ToCollectionExample.java)
```
[14, 13, 12, 11, 10]
```
Expand Down Expand Up @@ -533,7 +533,7 @@ Nested observables may be confusing at first, but they are a powerful construct
* Partitions of Data
* 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.
* Online Game servers
* 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.
* 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 signal that by completing.
* Financial data streams
* New markets or instruments may open and close during the day. These would then stream price information and could complete when the market closes.
* Chat Room
Expand All @@ -543,7 +543,7 @@ Nested observables may be confusing at first, but they are a powerful construct

### nest

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.
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 be the source observable and then terminate.

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

Expand Down
4 changes: 2 additions & 2 deletions Part 2 - Sequence Basics/5. Transformation of sequences.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Map: 2
Map: Error: java.lang.ClassCastException: Cannot cast java.lang.String to java.lang.Integer
```

If you would rather have such cases ignored, you can you the `ofType` method. This will filter our items that cannot be cast and then cast the sequence to the desired type.
If you would rather have such cases ignored, you can use the `ofType` method. This will filter our items that cannot be cast and then cast the sequence to the desired type.

```java
Observable<Object> values = Observable.just(0, 1, "2", 3);
Expand Down Expand Up @@ -182,7 +182,7 @@ TimeInterval: TimeInterval [intervalInMilliseconds=100, value=2]
TimeInterval: Completed
```

The information captured by `timestamp` and `timeInterval` is very useful for logging and debugging. It is Rx's way of aquiring information about the asynchronicity of sequences.
The information captured by `timestamp` and `timeInterval` is very useful for logging and debugging. It is Rx's way of acquiring information about the asynchronicity of sequences.

### materialize and dematerialize

Expand Down
Loading