Presentations from the june meeting of IDNDUG
http://ariely.info/Communities/IDNDUG/IDNDUG19thJune2013/tabid/171
The Reactive Extensions (Rx) is a library for composing asynchronous and event-based programs using observable sequences and LINQ-style query operators. Using Rx, developers represent asynchronous data streams with Observables, query asynchronous data streams using LINQ operators, andparameterize the concurrency in the asynchronous data streams using Schedulers. Simply put, Rx = Observables + LINQ + Schedulers
12. Converting Events and Asynchronous Methods
// Convert the TextChanged event to IObservable<string>
var input = (from evt in Observable.FromEventPattern(txt, âTextChangedâ)
select ((TextBox)evt.Sender).Text)
.Throttle(TimeSpan.FromSeconds(0.5))
.DistinctUntilChanged();
// Convert asynchronous proxy methods to Func<string, IObservable<string[]>>
var lookup = Observable.FromAsyncPattern<string, string[]>(svc.BeginLookup,
svc.EndLookup);
// Compose both sources using a query
var res = from term in input
from words in lookup(term).TakeUntil(input)
select words;
18. Stock Trade Analysis
MSFT
27.01
INTC
21.75
MSFT
27.96
MSFT
31.21
INTC
22.54
Company = MSFT
Increase = 0.104
from tick in ticks
group tick by tick.Symbol into company
from 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 }
INTC
20.98
MSFT
30.73
20. The Role of Schedulers
Parameterize Concurrency
//
// Runs a timer on the default scheduler
//
IObservable long
TimeSpan
//
// Every operator that introduces concurrency
// has an overload with an IScheduler
//
IObservable long
TimeSpan
IScheduler scheduler
21. The Role of Schedulers
var
Observable.Return
Scheduler.ThreadPool
"Answer = "
xs.ObserveOn(
frm )
xs.ObserveOn(new ControlScheduler(frm))
.Subscribe(x => lbl.Text = "Answer = " + x);
23. Operational Layering of Rx
public static IObservable<T> Return<T>(T value,
IScheduler scheduler)
{
return Observable.Create<T>(observer =>
{
// Serialize state to scheduler; return ability to cancel
return scheduler.Schedule(new { value, observer }, (_, x) =>
{
x.observer.OnNext(x.value);
x.observer.OnCompleted();
return Disposable.Empty; // No recursive work
});
});
}
24. Virtualizing Time for Testing
var scheduler = new TestScheduler();
var input = scheduler.CreateHotObservable(
OnNext(300, âBart De Smetâ),
OnNext(400, âErik Meijerâ),
OnCompleted<string>(500)
);
var results = scheduler.Start(() => from name in input
select name.Length);
results.Messages.AssertEqual(
OnNext(300, 12),
OnNext(400, 11),
OnCompleted<int>(500)
);
26. Summary
âą Tame your event streams using Rx and LINQ!
âą Download Rx today!
â Through http://www.microsoft.com/download (search for Rx SDK)
â Using NuGet @ www.nuget.org (search for Rx-Main)
âą Watch videos at http://channel9.msdn.com/tags/Rx
27. Related Content
RxJS
/* Only get the value from each key up */
var keyups = Rx.Observable.fromEvent(input, 'keyup')
.select(function (e) {
return e.target.value;
})
.where(function (text) {
return text.length > 2;
});
/* Now throttle/debounce the input for 500ms */
var throttled = keyups
.throttle(500 /* ms */);
/* Now get only distinct values, so we eliminate the arrows */
var distinct = keyups
.distinctUntilChanged();
RX++
void PrintPrimes(int n)
{
std::cout<<"Rx: first "<<n<<" primes squared"<<endl;
auto values = rxcpp::Range(2);
rxcpp::from(values)
.where(IsPrime)
.select([](int x) { return std::make_pair(x, x*x); })
.take(n)
.for_each(rxcpp::MakeTupleDispatch(
[](int p, int s)
{
cout<<p<<" =square=> "<<s<<endl;
}));
}
28. ReactiveUI
public class NewUserViewModel : ReactiveObject
{
// This is ReactiveUI's version of implementing INotifyPropertyChanged
string _Password,_PasswordConfirmation;
public string Password {
get { return _Password; }
set { this.RaiseAndSetIfChanged(x => x.Password, value); }
}
public string PasswordConfirmation {
get { return _PasswordConfirmation; }
set { this.RaiseAndSetIfChanged(x => x.PasswordConfirmation, value); }
}
ICommand OkCommand { get; protected set; }
public NewUserViewModel()
{
var canHitOk = this.WhenAny(
x => x.Password,
x => x.PasswordConfirmation,
(pass, confirm) => (pass.Value == confirm.Value && pass.Value.Length > 3));
OkCommand = new ReactiveCommand(canHitOk);
}
28
29. Resources
âą Channel 9
â Curing Your Event Processing Blues with Reactive Extensions (Rx) Bart De Smet
http://msdn.microsoft.com/enus/library/windowsazure/jj860549.aspx
âą IntroToRx.com
â IntroToRx.com is the online resource for getting started with
the Reactive Extensions to .Net.
âą www.reactiveui.com
https://github.com/reactiveui/ReactiveUI.Samples