A Brief Introduction To Reactive Extensions

24
a brief introduction to Reactive Extensions JAMES WORLD

Transcript of A Brief Introduction To Reactive Extensions

a brief introduction to

Reactive ExtensionsJAMES WORLD

A library for the composition of asynchronous and

event-based programs using observable

sequences and LINQ-style operators.

interactive streams

public interface IEnumerable<out T> : IEnumerable

{

IEnumerator<T> GetEnumerator();

}

public interface IEnumerator<out T> : IDisposable, IEnumerator

{

bool MoveNext();

T Current { get; }

void Reset();

}

composability

var openOrders =from customer in context.Customersfrom order in customer.Orderswhere customer.CustomerId == 101where order.Status == OrderStatus.Openselect order;

composability

var openOrders = context.Customers.Where(c => c.CustomerId == 101).SelectMany(c => c.Orders).Where(o => o.Status == OrderStatus.Open);

reactive streams

public interface IObservable<out T>{

IDisposable Subscribe(IObserver<T> observer);}

public interface IObserver<in T>{

void OnNext(T value);void OnError(Exception error);void OnCompleted();

}

OnNext OnError OnCompleted

creating observables

Observable.Return(42);

Observable.Never<int>();

Observable.Empty<int>();

Observable.Throw<int>(Exception error);

Observable.Timer(TimeSpan delay);

Observable.Interval(TimeSpan period);

Observable.Generate(…);

Observable.Create(…);

Social

media

RSS feeds

GPS

Server management

if “MSFT”

// Imperative code

/* what goes here? */

IObservable Quote

var

“MSFT”

var /* … */

MSFT

27.01

INTC

21.75

MSFT

27.96

MSFT

31.21

INTC

22.54

INTC

20.98

MSFT

30.73

from tick in ticks

MSFT

27.01

INTC

21.75

MSFT

27.96

MSFT

31.21

INTC

22.54

INTC

20.98

MSFT

30.73

27.01 27.96 31.21 30.73

21.75 22.54 20.98

from tick in ticks

group tick by tick.Symbol

MSFT

27.01

INTC

21.75

MSFT

27.96

MSFT

31.21

INTC

22.54

INTC

20.98

MSFT

30.73

from tick in ticks

group tick by tick.Symbol into companyfrom openClose in company.Buffer(2, 1)

[27.01, 27.96] [27.96, 31.21] [31.21, 30.73]

[21.75, 22.54] [22.54, 20.98]

MSFT

27.01

INTC

21.75

MSFT

27.96

MSFT

31.21

INTC

22.54

INTC

20.98

MSFT

30.73

from tick in ticksgroup tick by tick.Symbol into companyfrom openClose in company.Buffer(2, 1)

let diff = (openClose[1] – openClose[0]) / openClose[0]

0.034 0.104 -0.015

0.036 -0.069

MSFT

27.01

INTC

21.75

MSFT

27.96

MSFT

31.21

INTC

22.54

INTC

20.98

MSFT

30.73

from tick in ticksgroup tick by tick.Symbol into companyfrom openClose in company.Buffer(2, 1)let diff = (openClose[1] – openClose[0]) / openClose[0]

where diff > 0.1

0.034 0.104 -0.015

0.036 -0.069

MSFT

27.01

INTC

21.75

MSFT

27.96

MSFT

31.21

INTC

22.54

INTC

20.98

MSFT

30.73

from tick in ticksgroup tick by tick.Symbol into companyfrom openClose in company.Buffer(2, 1)let diff = (openClose[1] – openClose[0]) / openClose[0]where diff > 0.1

select new { Company = company.Key, Increase = diff }

Company = MSFT

Increase = 0.104

schedulers parameterize concurrency

IObservable long TimeSpanIScheduler scheduler

var Observable.Return Scheduler.ThreadPool"Answer = "

xs.ObserveOn(new ControlScheduler(frm)).Subscribe(x => lbl.Text = "Answer = " + x);

xs.ObserveOn( frm ).Subscribe(x => lbl.Text = "Answer = " + x);

async Task<string> GetHtmlAsync(Uri url)

{

var client = new WebClient();

var download = client.DownloadStringAsync(url);

var timeout = Task.Delay(TimeSpan.FromSeconds(30));

if (await Task.WhenAny(download, timeout) == timeout)

throw new TimeoutException();

var html = await download;

return html;

}

async without Rx

async Task<string> GetHtmlAsync(Uri url)

{

var client = new WebClient();

var download = client.DownloadStringAsync(url)

.ToObservable()

.Timeout(TimeSpan.FromSeconds(30));

var html = await download;

return html;

}

async with Rx

demo

@jamesw0rldlinkd.in/james-world

reactivex.io

www.introtorx.com

stackoverflow.com

rx-main rx-xaml rx-testing