Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
F# AND THE DLR Richard Minerich Senior Researcher at Bayard Rock  F# MVP of the Year RichardMinerich.com
The tools we use have a profound (and devious!) influence on our thinking habits, and, therefore, on our thinking abilities.  -- Edsger Dijkstra
Problems in Modern Programming Adding functionality to programs is difficult Exploring an existing code base is very difficult Architecture is hard Multi-threading is very hard Algorithms are difficult to both write and read later Why?  Our Object-Oriented heritage.
F# vs Dynamic F# Dynamic Languages Constructs conform to types and are fixed at runtime Types conform to constructs and change at runtime Data is immutable by default Generally data is mutable by default Protection from many kinds of mistakes No protection, even from typos Native interop with existing .NET Programs and Libraries Some work required for interop, but it’s not difficult Somewhat difficult to learn Very easy to learn
What is F#? F# is a Multi-Paradigm Language Functional with Some Type Extensions Allows for Object Oriented and Imperative Style Code F# is Interactive Command Line, Compile or Interactive Window F# is a .NET CLR Language In the box with Visual Studio 2010 Native Interop with every other .NET Language F# is Open Source But maintained and supported by Microsoft
If you love to code, you’ll love F#. Immutable Data Structures Tail Recursion Discriminated Unions Partial Application Function Composition Quotations Computation Expressions Statically Resolved Type Parameters
If you hate to code, you’ll love F#
Functional with Types? Functional + Types
F# is great for… Data Processing Technical Computing Algorithmic Programming Experimenting with Frameworks Implementing Critical Processes
And has the tools to support it.
Functional Programming in F# All statements return something let  x =  if  (y > 10)  then  10  else  y Everything is immutable by default let  rootList = [ 1; 2; 3 ] rootList @ [ 4 ] Chains, instead of discrete statements urls |> List.map download |> Async.Parallel
F# Programming Paradigms Recursion Pipelining Matching Function Composition Discriminated Unions
Recursion and TCO Using functions which call themselves. Function >  let   rec  badfib n =  if  n < 3  then  1  else  badfib (n - 1) + badfib (n - 2);; val badfib : int -> int >  let   rec   fib n1 n2 c =  if   c = 1  then   n2  else   fib n2 (n1 + n2) (c - 1);; val fib: int -> int -> int -> int
Higher Order Functions Pass Functions into Functions Return Functions from Functions Functions and Values are Interchangeable >  let  findBestSort list = if  List.length list < 1000  then  insertionSort else  quickSort;; val findBestSort : 'a list -> ('b list -> 'b list) when 'b : comparison Function Function Value Value Function Function  Function Value is a
Lambda Expressions A function doesn’t always have a name. >  let  addTwo = ( fun   x -> x + 2);; val  addTwo  : int -> int > List.map ( fun  x -> x * 2) [1; 2; 3; 4; 5] [2; 4; 6; 8; 10] let value = (fun v -> v) myFunc (fun v -> v) arg
Pipelining Push data through a series of functions. > [1; 2; 3; 4; 5] |> List.map ( fun  x -> x * 2) |> List.filter ( fun  x -> x % 3 <> 0);; [2; 4; 8; 10] Data function function function |> |> |> lambda lambda
Matching A switch statement on steroids.  match  num with | 1 -> &quot;One&quot; | x when x = 2 -> &quot;Two&quot; | _ -> &quot;Higher than I can count“ match exact case when | | -> variable with function -> function -> function name | -> function name predicate | active pattern
Function Composition Combine functions at runtime let  addOne x = x + 1 let  addTwo x = x + 2 let  addThree x = x + 3 let  addSix = addOne >> addTwo >> addThree ‘ a -> ‘b ‘ b -> ‘c ‘ a -> ‘c >> =
Discriminated Unions A type that can be one of several other types type name | signature name = name | signature type  Option<'a> = | Some of 'a | None let  printOutput opt =  match  opt with | Some (val) -> printfn “%A” val | None -> printfn “No Value” of of
And More! Statically   Resolved Type Parameters Special Record Types Object Expressions Comprehensions Computation Expressions Quotations
Try writing some AI http://is.gd/SilverlightAnts
Try F# at  tryfsharp.org
Find Some Functional Friends The Community For F# Online www.communityforfsharp.net The NYC F# User Group www.meetup.com/nyc-fsharp The Cambridge F# User Group fsug.org The San Francisco Bay Area F# User Group www.sfsharp.org F#unctional Londoners www.meetup.com/FSharpLondon
Read a Book
What is the DLR? The DLR is a layer on top of the CLR Designed for dynamic languages Enables interop with running CLR programs and .NET libraries Compatible Implementations Python, Ruby, Javascript, and Scheme It’s Fast IronPython is about 2x the speed of Python The Languages are Open Source Community maintained and not directly supported
The DLR is great for… Test Driven Development Web Development Using Code from Existing Dynamic Programs Writing your own Language Embedding in Applications
Dynamic in C#
DLR Architecture
Metaobject Visualization Users and programs interact with metaobjects, not directly with their backing objects as in C#, VB.NET or F#. control create invoke
Silverlight and the DLR
Embedded Languages in Your App
DLR Books and Resources http://www.silverlight.net/learn/dynamic-languages/ http://is.gd/DLRFAQ
Thank You Slides and code will be available on  RichardMinerich.com  later today. [email_address]

More Related Content

F# and the DLR

  • 1. F# AND THE DLR Richard Minerich Senior Researcher at Bayard Rock F# MVP of the Year RichardMinerich.com
  • 2. The tools we use have a profound (and devious!) influence on our thinking habits, and, therefore, on our thinking abilities. -- Edsger Dijkstra
  • 3. Problems in Modern Programming Adding functionality to programs is difficult Exploring an existing code base is very difficult Architecture is hard Multi-threading is very hard Algorithms are difficult to both write and read later Why? Our Object-Oriented heritage.
  • 4. F# vs Dynamic F# Dynamic Languages Constructs conform to types and are fixed at runtime Types conform to constructs and change at runtime Data is immutable by default Generally data is mutable by default Protection from many kinds of mistakes No protection, even from typos Native interop with existing .NET Programs and Libraries Some work required for interop, but it’s not difficult Somewhat difficult to learn Very easy to learn
  • 5. What is F#? F# is a Multi-Paradigm Language Functional with Some Type Extensions Allows for Object Oriented and Imperative Style Code F# is Interactive Command Line, Compile or Interactive Window F# is a .NET CLR Language In the box with Visual Studio 2010 Native Interop with every other .NET Language F# is Open Source But maintained and supported by Microsoft
  • 6. If you love to code, you’ll love F#. Immutable Data Structures Tail Recursion Discriminated Unions Partial Application Function Composition Quotations Computation Expressions Statically Resolved Type Parameters
  • 7. If you hate to code, you’ll love F#
  • 8. Functional with Types? Functional + Types
  • 9. F# is great for… Data Processing Technical Computing Algorithmic Programming Experimenting with Frameworks Implementing Critical Processes
  • 10. And has the tools to support it.
  • 11. Functional Programming in F# All statements return something let x = if (y > 10) then 10 else y Everything is immutable by default let rootList = [ 1; 2; 3 ] rootList @ [ 4 ] Chains, instead of discrete statements urls |> List.map download |> Async.Parallel
  • 12. F# Programming Paradigms Recursion Pipelining Matching Function Composition Discriminated Unions
  • 13. Recursion and TCO Using functions which call themselves. Function > let rec badfib n = if n < 3 then 1 else badfib (n - 1) + badfib (n - 2);; val badfib : int -> int > let rec fib n1 n2 c = if c = 1 then n2 else fib n2 (n1 + n2) (c - 1);; val fib: int -> int -> int -> int
  • 14. Higher Order Functions Pass Functions into Functions Return Functions from Functions Functions and Values are Interchangeable > let findBestSort list = if List.length list < 1000 then insertionSort else quickSort;; val findBestSort : 'a list -> ('b list -> 'b list) when 'b : comparison Function Function Value Value Function Function Function Value is a
  • 15. Lambda Expressions A function doesn’t always have a name. > let addTwo = ( fun x -> x + 2);; val addTwo : int -> int > List.map ( fun x -> x * 2) [1; 2; 3; 4; 5] [2; 4; 6; 8; 10] let value = (fun v -> v) myFunc (fun v -> v) arg
  • 16. Pipelining Push data through a series of functions. > [1; 2; 3; 4; 5] |> List.map ( fun x -> x * 2) |> List.filter ( fun x -> x % 3 <> 0);; [2; 4; 8; 10] Data function function function |> |> |> lambda lambda
  • 17. Matching A switch statement on steroids. match num with | 1 -> &quot;One&quot; | x when x = 2 -> &quot;Two&quot; | _ -> &quot;Higher than I can count“ match exact case when | | -> variable with function -> function -> function name | -> function name predicate | active pattern
  • 18. Function Composition Combine functions at runtime let addOne x = x + 1 let addTwo x = x + 2 let addThree x = x + 3 let addSix = addOne >> addTwo >> addThree ‘ a -> ‘b ‘ b -> ‘c ‘ a -> ‘c >> =
  • 19. Discriminated Unions A type that can be one of several other types type name | signature name = name | signature type Option<'a> = | Some of 'a | None let printOutput opt = match opt with | Some (val) -> printfn “%A” val | None -> printfn “No Value” of of
  • 20. And More! Statically Resolved Type Parameters Special Record Types Object Expressions Comprehensions Computation Expressions Quotations
  • 21. Try writing some AI http://is.gd/SilverlightAnts
  • 22. Try F# at tryfsharp.org
  • 23. Find Some Functional Friends The Community For F# Online www.communityforfsharp.net The NYC F# User Group www.meetup.com/nyc-fsharp The Cambridge F# User Group fsug.org The San Francisco Bay Area F# User Group www.sfsharp.org F#unctional Londoners www.meetup.com/FSharpLondon
  • 25. What is the DLR? The DLR is a layer on top of the CLR Designed for dynamic languages Enables interop with running CLR programs and .NET libraries Compatible Implementations Python, Ruby, Javascript, and Scheme It’s Fast IronPython is about 2x the speed of Python The Languages are Open Source Community maintained and not directly supported
  • 26. The DLR is great for… Test Driven Development Web Development Using Code from Existing Dynamic Programs Writing your own Language Embedding in Applications
  • 29. Metaobject Visualization Users and programs interact with metaobjects, not directly with their backing objects as in C#, VB.NET or F#. control create invoke
  • 32. DLR Books and Resources http://www.silverlight.net/learn/dynamic-languages/ http://is.gd/DLRFAQ
  • 33. Thank You Slides and code will be available on RichardMinerich.com later today. [email_address]

Editor's Notes

  1. C# is OO/Imperative, why is functional important?
  2. F# has a ton of new constructs to explore and will open your mind to completely new ways of doing things. Things you never thought possible before. You don’t need to understand of this stuff to enjoy F#, but it has a lot to explore if you’re into that kind of thing. Even if you don’t use F# professionally, just learning how these things work will make you a better programmer in general.
  3. If you care about getting things done quickly, and programming is just a means to an end F# can get you there quickly and safely.
  4. More, Better, Natively Concurrent Code in Less Time
  5. F# has a lot of great graphics frameworks. You can make beautiful graphics with very little code. FsChart (left), VSLab (middle top), Tomas’s F# Library for Learning Graphics (top right), Jon Harrop F# for Visualization (bottom right) Tomas’s Graphics Lib: https://github.com/tpetricek/Documents/tree/master/Talks%202011/Teaching%20F%23%20(Community%20for%20F%23)
  6. It fits together like puzzle pieces.
  7. You can perform any type of iteration with a recursive function. F# optimizes a special type of recursive functions into loops using a method called tail call optimization. Tail call optimization
  8. Lambda expressions are great because they reduce our need to make imperative style variable assignments. Small functions can just be included inline.
  9. Commonly used with higher order functions and lambda expressions
  10. Sticker question Answer: “Less than ten” Match on types, contents of types, strings, lists…. Very flexible
  11. This is used to avoid the problem of nulls New values not in a match will cause warnings (or even failure if the compiler is told to do so) They can even be recursive and be used to build strongly typed tree structures
  12. I wrote an ant colony AI simulation in Silverlight with uploadable AI. Give it a go when you get some time.
  13. You only need parse and lex your way to an expression tree. The DLR takes care of everything from there.
  14. Slide care of Kevin Hazzard
  15. You can write your whole Silverlight application without a single line of compiled code