1 / 34

F# Information Rich Programming

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

dard
Download Presentation

F# Information Rich Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. F# Information Rich Programming Donna Malayeri Program Manager, Visual Studio F#

  2. What’s in this talk? Intro to F# Language features for solving real-world problems F# Type Provider demos Future directions

  3. F# Tagline F# is a practical, functional-first language that lets you write simplecode to solve complexproblems

  4. Simplicity

  5. 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>)

  6. 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#

  7. F# is declarative

  8. Demo 1: Working with Sequences

  9. What about real-world tasks?

  10. F# 3.0Information Rich Programming

  11. Two propositions

  12. Proposition 1:We live in an information society

  13. Proposition 2:Our languages are information sparse

  14. A Big Problem

  15. 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

  16. But… Data sources often have rich schemas and associated data definitions Static types should make your experience better, not worse!

  17. Programming the web Why this matters

  18. F# Type Providers:IntelliSense for data

  19. Demo 2: Freebase

  20. Demo

  21. // 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)

  22. 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!

  23. Demo 3:Web service with multiple data sources

  24. Demo

  25. F# Matches the Math List.map2 (-) x y |> List.map square |> List.map2 (*) weights |> List.sum |> sqrt

  26. 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

  27. Put program logic in a reusable library Portable libraries Silverlight Metro

  28. 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

  29. Computation in the Cloud

  30. 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

  31. Demo

More Related