340 likes | 470 Views
Join Donna Malayeri as she explores F#, a practical functional-first programming language designed to tackle complex problems with simplicity. This talk highlights F# language features, including type providers that enhance IntelliSense for richer data handling, and showcases dynamic demos involving real-world tasks. Learn about F#’s practicality, type safety, and future directions while seeing how it integrates smoothly into Visual Studio for effective programming solutions.
E N D
F# Information Rich Programming Donna Malayeri Program Manager, Visual Studio F#
What’s in this talk? Intro to F# Language features for solving real-world problems F# Type Provider demos Future directions
F# Tagline F# is a practical, functional-first language that lets you write simplecode to solve complexproblems
Simplicity abstractclassCommand{ publicvirtualvoid Execute(); } abstractclassRoverCommand: Command { protected Rover Rover { get; privateset; } publicRoverCommand(MarsRover rover){ this.Rover = rover; } } classBrakeCommand: RoverCommand{ publicBrakeCommand(Rover rover): base(rover) { } publicoverridevoid Execute(){ Rover.Accelerate(-1.0); } } classTurnLeftCommand: RoverCommand{ publicTurnLeftCommand(Rover rover): base(rover) {} publicoverridevoid Execute(){ Rover.Rotate(-5.0); } } C# Functions as values F# type Command = Rover -> unit letBrakeCommand = fun rover ->rover.Accelerate(-1.0) letTurnLeftCommand = fun rover ->rover.Rotate(-5.0<degs>)
Simplicity Functional data letswap (x, y) = (y, x) let rotations (x, y, z) = [ (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) { returnnew Tuple<U,T>(t.Item2, t.Item1) } ReadOnlyCollection<Tuple<T,T,T>> Rotations<T> (Tuple<T,T,T> t) { newReadOnlyCollection<int> (new Tuple<T,T,T>[] { new Tuple<T,T,T>(t.Item1,t.Item2,t.Item3); new Tuple<T,T,T>(t.Item3,t.Item1,t.Item2); new Tuple<T,T,T>(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#
Challenges Impedance mismatch with statically-typed languages Need to manually integrate codegen tools with build process, source control, etc. No elegant way to handle schema change Loss of type information due to up-casts to Object, or even have to just parse strings
But… Data sources often have rich schemas and associated data definitions Static types should make your experience better, not worse!
Programming the web Why this matters
// 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 openSystem.Net openSystem.Text openSystem.Web openSystem.Security.Authentication openSystem.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>] typeChemicalElement = { [<field: DataMember(Name="name") >] Name:string [<field: DataMember(Name="boiling_point") >] BoilingPoint:string [<field: DataMember(Name="atomic_mass") >] AtomicMass:string } letQuery<'T>(query:string) : 'T = let query = query.Replace("'","\"") letqueryUrl = sprintf "http://api.freebase.com/api/service/mqlread?query=%s" "{\"query\":"+query+"}" let request : HttpWebRequest = downcastWebRequest.Create(queryUrl) request.Method <- "GET" request.ContentType <- "application/x-www-form-urlencoded" let response = request.GetResponse() let result = try use reader = newStreamReader(response.GetResponseStream()) reader.ReadToEnd(); finally response.Close() let data = Encoding.Unicode.GetBytes(result); let stream = newMemoryStream() stream.Write(data, 0, data.Length); stream.Position <- 0L letser = Json.DataContractJsonSerializer(typeof<Result<'T>>) let result = ser.ReadObject(stream) :?> Result<'T> ifresult.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)
Demo 2 summary • Can program against web-scale schematized data • code gen would never work here! • Don’t have to wait for codegen or code to compile • With typechecking! • Can detect schemachange • With great Visual Studio tooling!
F# Matches the Math List.map2 (-) x y |> List.map square |> List.map2 (*) weights |> List.sum |> sqrt
Demo 3 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
Put program logic in a reusable library Portable libraries Silverlight Metro
What’s next for F#? • Cloud, cloud cloud! • Make F# programming on Azure even easier • Can use F# computation expressions to add new syntax in a library • for example, cloud { … } • F# asyncand queryexpressions are not built-in syntax—they use computation expressions
Cloud Numerics • Extensive library of numerical algorithms • Ranges from basic math to advanced statistics to linear algebra • Supports distributed arrays and computation • Can deploy to Azure when lots of compute resources are needed