Upload
jocelin-charles
View
216
Download
2
Embed Size (px)
Citation preview
F# 3.0: Data, Services, Web, Cloud, at Your FingertipsDustin CampbellSenior Program ManagerMicrosoft Corporation
DEV338
What’s in this Talk?
Intro and brief history of F#
F# primer
F# 3.0 features to address a real-world problem
F# Type Providers Demos!
What is F#?
F# is a practical, functional-first language
that lets you write simple code
to solve complex problems
A Brief History…
Began as a project at Microsoft Research (2002-2006)
Based on the functional language OCaml
Brainchild of Don Syme, key designer of .NET generics
F# 2.0 became a full-fledged product in Visual Studio 2010
Became popular for analytical computing
Is used by industries such as banking, insurance, energy (and more!)
More about F#
100% .NET Language
Full interop with other .NET languagesCan call into .NET libraries and frameworks
Can be called from any other .NET language
Strongly typed, full type inferenceF# 2.0: Units of Measure prevent mixing kilograms and ounces, dollars and euros (or your own custom units)
F# 3.0: Type Providers allow you to introduce new types and members “on the fly”
Simplicity
abstract class RoverCommand { protected Rover Rover { get; private set; } public RoverCommand(Rover rover) { this.Rover = rover; }
abstract void Execute();}
class BrakeCommand : RoverCommand { public BrakeCommand(Rover rover) : base(rover) { }
public override void Execute() { Rover.Accelerate(-1.0); }}
class TurnLeftCommand : RoverCommand { public TurnLeftCommand(Rover rover) : base(rover) { }
public override void Execute() { Rover.Rotate(-5.0); }}
type RoverCommand = Command of (Rover -> unit) let BrakeCommand = Command(fun rover -> rover.Accelerate(-1.0<m/s>)) let TurnLeftCommand = Command(fun rover -> rover.Rotate(-5.0<degs>))
F#
C#
SimplicityFunctions as values
let swap (x, y) = (y, x)
let rotations (x, y, z) = seq [ (x, y, z) (z, x, y) (y, z, x) ]
let reduce f (x, y, z) = f x + f y + f z
Tuple<U,T> Swap<T,U>(Tuple<T,U> t){ return Tuple.Create(t.Item2, t.Item1)} IEnumerable<Tuple<T,T,T>> Rotations<T>(Tuple<T,T,T> t) { yield return Tuple.Create(t.Item1, t.Item2, t.Item3); yield return Tuple.Create(t.Item3, t.Item1, t.Item2); yield return Tuple.Create(t.Item2, t.Item3, t.Item1);} int Reduce<T>(Func<T,int> f, Tuple<T,T,T> t) { return f(t.Item1) + f(t.Item2) + f (t.Item3); }
F#C#
SimplicityFunctional data
F# is declarative
www.flickr.com/photos/bigbirdz
www.flickr.com/photos/digitalcolony
www.flickr.com/photos/richardaustin
www.flickr.com/photos/bigiain
Demo
Simplicity – an F# Primer
What about real-world tasks?
F# 3.0Information Rich Programming
Two propositions
Proposition 1We live in an information society
Proposition 2Our languages are information sparse
This is a problem
Challenges
Impedance mismatch with statically-typed languagesNeed to manually integrate code-gen tools with build process, source control, etc.No elegant way to handle schema changeLoss of type information due to up-casts to Object, or even have to just parse strings
Plus…
Data sources often have rich schemas and associated data definitionsStatic types should make your experience better, not worse!
Why This MattersProgramming the web
source: blog.programmableweb.com
F# Type Providers:IntelliSense for data
Demo
Freebase
http://wiki.freebase.com/wiki/Main_Page
Demo
Freebase
// Freebase.fsx // Example of reading from freebase.com in F# // by Jomo Fisher #r "System.Runtime.Serialization" #r "System.ServiceModel.Web" #r "System.Web" #r "System.Xml" open System open System.IO open System.Net open System.Text open System.Web open System.Security.Authentication open System.Runtime.Serialization [<DataContract>] type Result<'TResult> = { [<field: DataMember(Name="code") >] Code:string [<field: DataMember(Name="result") >] Result:'TResult [<field: DataMember(Name="message") >] Message:string } [<DataContract>] type ChemicalElement = { [<field: DataMember(Name="name") >] Name:string [<field: DataMember(Name="boiling_point") >] BoilingPoint:string [<field: DataMember(Name="atomic_mass") >] AtomicMass:string }
let Query<'T>(query:string) : 'T = let query = query.Replace("'","\"") let queryUrl = sprintf
"http://api.freebase.com/api/service/mqlread?query=%s" "{\"query\":"+query+"}"
let request : HttpWebRequest = downcast WebRequest.Create(queryUrl) request.Method <- "GET" request.ContentType <- "application/x-www-form-urlencoded" let response = request.GetResponse() let result = try use reader = new StreamReader(response.GetResponseStream()) reader.ReadToEnd(); finally response.Close() let data = Encoding.Unicode.GetBytes(result); let stream = new MemoryStream() stream.Write(data, 0, data.Length); stream.Position <- 0L let ser = Json.DataContractJsonSerializer(typeof<Result<'T>>) let result = ser.ReadObject(stream) :?> Result<'T> if result.Code <> "/api/status/ok" then raise (InvalidOperationException(result.Message)) else result.Result let elements = Query<ChemicalElement array>("[{'type':'/chemistry/chemical_element','name':null,'boiling_point':null,'atomic_mass':null}]") elements |> Array.iter(fun element -> printfn "%A" element)
Freebase Demo Summary
Can program against web-scale schematized data
Code-gen would never work here!
No waiting for code-gen or compilations
With typechecking!Can detect schema change
With great Visual Studio tooling!
Demo
Web service and multiple data sources
Demo
Where should I live?
Demo Summary
Can easily program against multiple data sources using type providers
Access web services, databases, etc., using a uniform interface
F# works well for the program logic
F# 3.0Services, data, web, cloud—at your fingertips
Type providers provide an integrated solution for accessing data sources
Keeps the experience code-focused
Provides a consistent and uniform programming experience
Schema change integrates with IDE and build system
Tools matter!
Try it now!
F# 3.0 is in Visual Studio 2012 RC
New Project Visual F# F# TutorialLearn the language from within Visual Studio!
For more, visit http://fsharp.net
DEV Track Resources
Visual Studio Home Page :: http://www.microsoft.com/visualstudio/en-us
Jason Zander’s Blog :: http://blogs.msdn.com/b/jasonz/
Facebook :: http://www.facebook.com/visualstudio
Twitter :: http://twitter.com/#!/visualstudio
Somasegar’s Blog :: http://blogs.msdn.com/b/somasegar/
Resources
Connect. Share. Discuss.
http://europe.msteched.com
Learning
Microsoft Certification & Training Resources
www.microsoft.com/learning
TechNet
Resources for IT Professionals
http://microsoft.com/technet
Resources for Developers
http://microsoft.com/msdn
Evaluations
http://europe.msteched.com/sessions
Submit your evals online
© 2012 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.