Skip to content

Commit

Permalink
Merge pull request #9 from Froussios/Example-quality-control
Browse files Browse the repository at this point in the history
Polished examples
  • Loading branch information
Froussios committed Jul 6, 2015
2 parents 4f1ea02 + 590b2f5 commit f0b981d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 26 deletions.
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 @@ -47,7 +47,7 @@ Output:
While this can be useful, more common use is to transform values from one type to another. In this example we transform integer values to characters.

```C#
Observable.Range(1, 5);
Observable.Range(1, 5)
.Select(i =>(char)(i + 64))
.Dump("char");
```
Expand Down Expand Up @@ -315,7 +315,7 @@ This last example better illustrates how `SelectMany` can take a single value an
```C#
Func<int, char> letter = i => (char)(i + 64);
Observable.Return(1)
.SelectMany(i => Observable.Return(letter(i)));
.SelectMany(i => Observable.Return(letter(i)))
.Dump("SelectMany");
```

Expand Down
8 changes: 4 additions & 4 deletions Part 3 - Taming the sequence/1. Side effects.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ The class now looks much better. The improvement, however, is only cosmetic. The

```C#
var repo = new ObscuredLeakinessLetterRepo();
var good = repo.GetLetters();
var evil = repo.GetLetters();
var good = repo.Letters;
var evil = repo.Letters;
good.Subscribe(
Console.WriteLine);
//Be naughty
Expand All @@ -350,9 +350,9 @@ C
The fix to this problem is quite simple. By applying the `AsObservable` extension method, the _letters field will be wrapped in a type that only implements IObservable<T>.

```C#
public IObservable<string> GetLetters()
public IObservable<string> Letters
{
return _letters.AsObservable();
get { return _letters.AsObservable(); }
}
```

Expand Down
2 changes: 1 addition & 1 deletion Part 3 - Taming the sequence/3. Advanced error handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Here S1 represents the first sequence that ends with an error (X). S2 is the con
```C#
var source = new Subject<int>();
var result = source.Catch(Observable.Empty<int>());
result.Dump("Catch"););
result.Dump("Catch");
source.OnNext(1);
source.OnNext(2);
source.OnError(new Exception("Fail!"));
Expand Down
6 changes: 3 additions & 3 deletions Part 3 - Taming the sequence/4. Combining sequences.md
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,8 @@ var systemStatus = webServerStatus
.CombineLatest(
databaseStatus,
(webStatus, dbStatus) => webStatus && dbStatus)
.DistinctUntilChanged()
.StartWith(false);
.StartWith(false)
.DistinctUntilChanged();
```

### Zip
Expand Down Expand Up @@ -680,7 +680,7 @@ It may sound very complex, but comparing some code samples should make it easier

To `Zip` three sequences together, you can either use Zip methods chained together like this:

```C#`
```C#
var one = Observable.Interval(TimeSpan.FromSeconds(1)).Take(5);
var two = Observable.Interval(TimeSpan.FromMilliseconds(250)).Take(10);
var three = Observable.Interval(TimeSpan.FromMilliseconds(150)).Take(14);
Expand Down
19 changes: 8 additions & 11 deletions Part 3 - Taming the sequence/5. Time-shifted sequences.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,21 +279,18 @@ To illustrate this we will create a buffering scheme where we open a new buffer
```C#
Observable.Interval(TimeSpan.FromMilliseconds(100))
.Buffer(
Observable.Interval(TimeSpan.FromMilliseconds(100))
.Select(i => i+1)
.StartWith(0), // emit "0" at time 0
Observable.Timer(TimeSpan.Zero, TimeSpan.FromMilliseconds(100)),
(v) => Observable.Timer(TimeSpan.FromMilliseconds(100 * v)))
.Subscribe(b => Console.WriteLine(String.Join(", ", b)));
```
Output
```
0
1, 2
2, 3, 4
3, 4, 5, 6
4, 5, 6, 7, 8
5, 6, 7, 8, 9, 10
1
2, 3
3, 4, 5
4, 5, 6, 7
5, 6, 7, 8, 9
...
```

Expand All @@ -313,7 +310,7 @@ x
--------------------x
```

Note that running such time-sensitive examples will probably produce similar but different results than what we present here, because timed operations in C# are not guaranteed to be accurate.
Note that running such time-sensitive examples will probably produce similar but different results than what we present here, because timed operations in C# are not guaranteed to be accurate. Even theoretically, there are limitations to timed scheduling: looking at the first marble diagram, should the second buffer include `0`, `1` or both? It touches both events, but it just happens here that the scheduled opening of the buffer is executed immediately after the emission of onNext in the source.


## Delay
Expand Down Expand Up @@ -525,7 +522,7 @@ public static IObservable<TSource> Timeout<TSource>(
If we provide a `TimeSpan` and no values are produced within that time span, then the sequence fails with a `TimeoutException`.

```C#
var source = Observable.Interval(TimeSpan.FromMilliseconds(100)).Take(10)
var source = Observable.Interval(TimeSpan.FromMilliseconds(100)).Take(5)
.Concat(Observable.Interval(TimeSpan.FromSeconds(2)));
var timeout = source.Timeout(TimeSpan.FromSeconds(1));
timeout.Subscribe(
Expand Down
10 changes: 5 additions & 5 deletions Part 3 - Taming the sequence/6. Hot and Cold observables.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void SimpleColdSample()
var period = TimeSpan.FromSeconds(1);
var observable = Observable.Interval(period);
observable.Subscribe(i => Console.WriteLine("first subscription : {0}", i));
Thread.Sleep(period);
Thread.Sleep(period + TimeSpan.FromMilliseconds(50));
observable.Subscribe(i => Console.WriteLine("second subscription : {0}", i));
Console.ReadKey();
/* Output:
Expand All @@ -160,7 +160,7 @@ var period = TimeSpan.FromSeconds(1);
var observable = Observable.Interval(period).Publish();
observable.Connect();
observable.Subscribe(i => Console.WriteLine("first subscription : {0}", i));
Thread.Sleep(period);
Thread.Sleep(period + TimeSpan.FromMilliseconds(50));
observable.Subscribe(i => Console.WriteLine("second subscription : {0}", i));
```

Expand All @@ -179,7 +179,7 @@ In the example above, the observable variable is an `IConnectableObservable<T>`,
var period = TimeSpan.FromSeconds(1);
var observable = Observable.Interval(period).Publish();
observable.Subscribe(i => Console.WriteLine("first subscription : {0}", i));
Thread.Sleep(period);
Thread.Sleep(period + TimeSpan.FromMilliseconds(50));
observable.Subscribe(i => Console.WriteLine("second subscription : {0}", i));
observable.Connect();
```
Expand Down Expand Up @@ -343,11 +343,11 @@ Output:
Press any key to subscribe
Publishing 0
Publishing 1
Press any key to unsubscribe.
Publishing 2
Publishing 3
Press any key to unsubscribe.
Publishing 4
subscription : 4
subscription: 4
Press any key to exit.
```

Expand Down

0 comments on commit f0b981d

Please sign in to comment.