ft10 de smet
TRANSCRIPT
LINQ, Take TwoRealizing the LINQ to Everything Dream
Bart J.F. De SmetSoftware Development EngineerMicrosoft Corporation
What’s LINQ?A historical perspective
Censored
5 years ago
Little recent innovation
Where’s the cloud?
Essential LINQLanguage Integrated MonadsWhy?
What?
How?
Essential LINQThe monadic Bind operator
IEnumerable<T>IQueryable<T>
new[]{ 42 }
SelectMany IEnumerable<T> SelectMany<T, R>( this IEnumerable<T> source, Func<T, IEnumerable<R>> selector)
Could there be more?
Also see www.codeplex.com/LINQSQO for “Project MinLINQ”
Essential LINQMaybe monad (for fun and no profit)
from _ in namefrom s in _.ToUpper()select s
name.SelectMany( _ => _.ToUpper(), s => s)Compiler
Null-propagating dotstring s =
name?.ToUpper();
Syntactic sugar
One single library function suffices
Essential LINQ
Bart J.F. De SmetSoftware Development EngineerCloud Programmability Team
demo
Query Providers RevisitedWhy do we have IQueryable<T>?
var src = new QueryProvider<Product>();
var res = from p in src where p.Price > 100m group p by p.Category;
foreach (var p in res) Console.WriteLine(p);
ImplementsIQueryable<T>
Compiles fine
Does it really have to be a runtime check?
Query Providers RevisitedLeveraging the query pattern
var res = from p in src
where p.Price > 100m
group p by p.Category;
var res = src
.Where(p => p.Price > 100m)
.GroupBy(p => p.Category);
Syntactic sugar
Can be instance methods
Source<T>
Filtered<T>
Projected<T>
Where Select
No GroupBy “edge”
Query Providers RevisitedTaking it one step further
var res = from tweet in twitter
where tweet.AboutFromLocation
== “Bart” From From
AboutLocation
== “LINQ”About
where tweet. About
Query “learns”
class Twitter{ public TwitterByFrom Where(Func<TweetAboutFromLoc, FilterFrom> filter); // Other filter methods}
Recipe
Method overloadsType state machineOperator overloads
“Has a” type From operator overloadingclass TwitterByFrom{ public TwitterByAboutFrom Where(Func<TweetAboutLoc, FilterAbout> filter); // Other filter methods // Fields with current filters}
select tweet;
Custom syntax trees
Query Providers Revisited
Bart J.F. De SmetSoftware Development EngineerCloud Programmability Team
demo
Asynchronous Data Access
Rx is a library for composing asynchronous and event-based
programs using observable collections.
( 𝑓 ◦𝑔)(𝑥 )= 𝑓 (𝑔(𝑥 ))
Queries! LINQ!
Way simpler with Rx
• .NET 3.5 SP1 and 4.0, Silverlight 3 and 4
• JavaScript (RxJS)• XNA 3.1 for XBOX and Zune• Windows Phone 7
Download at MSDN DevLabs
Asynchronous Data AccessPush-based data retrieval
Environment
MoveN
ext
Got next?
Application
On
Nex
t
Have next!
IEnumerable<T>
IEnumerator<T>
IObservable<T>
IObserver<T>
Inte
racti
ve R
eactiv
e
interface IObservable<out T>{ IDisposable Subscribe(IObserver<T> observer);}
interface IObserver<in T>{ void OnNext(T value); void OnError(Exception ex); void OnCompleted();}
Asynchronous Data AccessEssential interfaces
Both interfaces ship in the .NET 4 BCL
Fix
ed
(MS
IL)
Tran
sla
tab
le(E
xp
ressio
n
trees)
ToQueryable
ToObservable
ToEnumerable
AsQ
uery
able
AsE
num
era
ble A
sQbse
rvable
AsO
bse
rvable
Pull(interactive)
Push(reactive)
Concurre
ncy
(ISch
eduler)
LINQ to *.*
What?
Where
?
How
?
Worker pools Message loopsThreads Distributed
Duality
Hom
o-
icon
ic
IQbservable<T>LINQ to WMI Events
ToQbservableIQueryable<T>
LINQ to SQL
IEnumerable<T>
LINQ to Objects
IObservable<T>
LINQ to Events
interface IQbservable<out T> : IObservable<T>{ Expression Expression { get; } Type ElementType { get; } IQbservableProvider Provider { get; }}
interface IQbservableProvider{ IQbservable<R> CreateQuery<R>(Expression expression);}
Asynchronous Data AccessIQbservable<T>
IObservable<T>
We welcome semantic
discussions.
Extended role for some
operators No Execute method
LINQ to WMI Events (WQL)
Bart J.F. De SmetSoftware Development EngineerCloud Programmability Team
demo
Asynchronous Data AccessC# 5.0 and VB 11.0 “await”
One versus many?IObservable<T> interface – many resultsTask<T> concrete class – one result
Task<T> based language featureRx bridge by implementing the “await pattern”IAsyncEnumerable<T>
Asynchronous Bridge for C# and VB “await”
Bart J.F. De SmetSoftware Development EngineerCloud Programmability Team
announcing
interface IScheduler{ DateTimeOffset Now { get; } IDisposable Schedule(Action action); IDisposable Schedule(Action action, TimeSpan dueTime);}
Where execution happensIScheduler interface Introductio
n of concurrenc
y
IEnumerable<T> IObservable<T>
Synchronous Asynchronous
ToObservable
ToEnumerable
System.Concurrency in System.CoreEx
var res = from word in input.DistinctUntilChanged() .Throttle(TimeSpan.FromSeconds(0.5)) from words in lookup(word) select words;
Where execution happensIScheduler specifies “where”Imported
TextBox TextChanged
event
Asynchronous web service callUse of
scheduler to synchronize
res.ObserveOn(new ControlScheduler(frm)).Subscribe(words => { lst.Items.Clear(); lst.Items.AddRange((from word in words select word.Word).ToArray());});
res.Subscribe(words => { lst.Items.Clear(); lst.Items.AddRange((from word in words select word.Word).ToArray());});
Indicates where
things run
var ctx = new NorthwindDataContext(); var res = from product in ctx.Products where product.Price > 100m select product.Name;
Where execution happensIScheduler parameterization
Baked in notion of “where”?
foreach (var product in res.RemoteOn(new SqlScheduler(“server”))) // Process product
Decoupled “what” from
“where”
Entry-point for the
schemaCustom schedulers could be very rich (e.g. server farm)
from ticker in stockswhere ticker.Symbol == “MSFT”select ticker.Quote
Where execution happensExpression tree remoting
Rx .NET
RxJS stocks.Where(function (t) { return t.Symbol == “MSFT”; }).Select(function (t) { return t.Quote; })
JSON serializer
Observable data source
Retargeting to AJAX
Remoting of Query Operators
Bart J.F. De SmetSoftware Development EngineerCloud Programmability Team
demo
LINQ to the unexpectedConstraint solvingModel[ Decisions[Reals, SA, VZ], Goals[ Minimize[20 * SA + 15 * VZ] ], Constraints[ C1 -> 0.3 * SA + 0.4 * VZ >= 2000, C2 -> 0.4 * SA + 0.2 * VZ >= 1500, C3 -> 0.2 * SA + 0.3 * VZ >= 500, C4 -> SA <= 9000, C5 -> VZ <= 6000, C6 -> SA >= 0, C7 -> VZ >= 0 ]]
from m in ctx.CreateModel(new { vz = default(double), sa = default(double)})where 0.3 * m.sa + 0.4 * m.vz >= 2000 && 0.4 * m.sa + 0.2 * m.vz >= 1500 && 0.2 * m.sa + 0.3 * m.vz >= 500where 0 <= m.sa && m.sa <= 9000 && 0 <= m.vz && m.vz <= 6000orderby 20 * m.sa + 15 * m.vzselect m
To compute call Solve
Non-persistent
Cost / barrel
Max # barrels
Min # barrels
Refin
em
en
t %
LINQ to the unexpectedJoining with reactive sources
from costSA in GetPriceMonitor("SA")from costVZ in GetPriceMonitor("VZ")
from m in ctx.CreateModel(new { vz = default(double), sa = default(double) })where 0.3 * m.sa + 0.4 * m.vz >= 2000 && 0.4 * m.sa + 0.2 * m.vz >= 1500 && 0.2 * m.sa + 0.3 * m.vz >= 500where 0 <= m.sa && m.sa <= 9000 && 0 <= m.vz && m.vz <= 6000
orderby costSA * m.sa + costVZ * m.vzselect m
Observabledata sources
Sele
ctM
an
y
Parametersto decision
Subscribe here!
Theorem Solving using Z3
Bart J.F. De SmetSoftware Development EngineerCloud Programmability Team
demo
What’s LINQ?A futuristic perspective
Censored
Next 5 years
Rx and Ix are here!
Remoting
Async
Solvers
Toolkits
Scheduler
© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to
be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.