F# and the DLR


Published on

Dev Day for NSWC Dahlgren

Published in: Technology
1 Comment
No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • C# is OO/Imperative, why is functional important?
  • 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.
  • 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.
  • More, Better, Natively Concurrent Code in Less Time
  • 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)
  • It fits together like puzzle pieces.
  • 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
  • Lambda expressions are great because they reduce our need to make imperative style variable assignments. Small functions can just be included inline.
  • Commonly used with higher order functions and lambda expressions
  • Sticker question Answer: “Less than ten” Match on types, contents of types, strings, lists…. Very flexible
  • 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
  • I wrote an ant colony AI simulation in Silverlight with uploadable AI. Give it a go when you get some time.
  • You only need parse and lex your way to an expression tree. The DLR takes care of everything from there.
  • Slide care of Kevin Hazzard
  • You can write your whole Silverlight application without a single line of compiled code
  • F# and the DLR

    1. 1. F# AND THE DLR Richard Minerich Senior Researcher at Bayard Rock F# MVP of the Year RichardMinerich.com
    2. 2. The tools we use have a profound (and devious!) influence on our thinking habits, and, therefore, on our thinking abilities. -- Edsger Dijkstra
    3. 3. Problems in Modern Programming <ul><li>Adding functionality to programs is difficult </li></ul><ul><li>Exploring an existing code base is very difficult </li></ul><ul><li>Architecture is hard </li></ul><ul><li>Multi-threading is very hard </li></ul><ul><li>Algorithms are difficult to both write and read later </li></ul><ul><li>Why? Our Object-Oriented heritage. </li></ul>
    4. 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. 5. What is F#? <ul><li>F# is a Multi-Paradigm Language </li></ul><ul><ul><li>Functional with Some Type Extensions </li></ul></ul><ul><ul><li>Allows for Object Oriented and Imperative Style Code </li></ul></ul><ul><li>F# is Interactive </li></ul><ul><ul><li>Command Line, Compile or Interactive Window </li></ul></ul><ul><li>F# is a .NET CLR Language </li></ul><ul><ul><li>In the box with Visual Studio 2010 </li></ul></ul><ul><ul><li>Native Interop with every other .NET Language </li></ul></ul><ul><li>F# is Open Source </li></ul><ul><ul><li>But maintained and supported by Microsoft </li></ul></ul>
    6. 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. 7. If you hate to code, you’ll love F#
    8. 8. Functional with Types? Functional + Types
    9. 9. F# is great for… <ul><li>Data Processing </li></ul><ul><li>Technical Computing </li></ul><ul><li>Algorithmic Programming </li></ul><ul><li>Experimenting with Frameworks </li></ul><ul><li>Implementing Critical Processes </li></ul>
    10. 10. And has the tools to support it.
    11. 11. Functional Programming in F# <ul><li>All statements return something </li></ul><ul><li>let x = if (y > 10) then 10 else y </li></ul><ul><li>Everything is immutable by default </li></ul><ul><li>let rootList = [ 1; 2; 3 ] </li></ul><ul><li>rootList @ [ 4 ] </li></ul><ul><li>Chains, instead of discrete statements </li></ul><ul><li>urls |> List.map download |> Async.Parallel </li></ul>
    12. 12. F# Programming Paradigms <ul><li>Recursion </li></ul><ul><li>Pipelining </li></ul><ul><li>Matching </li></ul><ul><li>Function Composition </li></ul><ul><li>Discriminated Unions </li></ul>
    13. 13. Recursion and TCO <ul><li>Using functions which call themselves. </li></ul>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. 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. 15. Lambda Expressions <ul><ul><li>A function doesn’t always have a name. </li></ul></ul>> 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. 16. Pipelining <ul><ul><li>Push data through a series of functions. </li></ul></ul>> [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. 17. Matching <ul><ul><li>A switch statement on steroids. </li></ul></ul>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. 18. Function Composition <ul><li>Combine functions at runtime </li></ul>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. 19. Discriminated Unions <ul><li>A type that can be one of several other types </li></ul>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. 20. And More! <ul><li>Statically Resolved Type Parameters </li></ul><ul><li>Special Record Types </li></ul><ul><li>Object Expressions </li></ul><ul><li>Comprehensions </li></ul><ul><li>Computation Expressions </li></ul><ul><li>Quotations </li></ul>
    21. 21. Try writing some AI http://is.gd/SilverlightAnts
    22. 22. Try F# at tryfsharp.org
    23. 23. Find Some Functional Friends <ul><li>The Community For F# Online </li></ul><ul><li>www.communityforfsharp.net </li></ul><ul><li>The NYC F# User Group </li></ul><ul><li>www.meetup.com/nyc-fsharp </li></ul><ul><li>The Cambridge F# User Group </li></ul><ul><li>fsug.org </li></ul><ul><li>The San Francisco Bay Area F# User Group </li></ul><ul><li>www.sfsharp.org </li></ul><ul><li>F#unctional Londoners </li></ul><ul><li>www.meetup.com/FSharpLondon </li></ul>
    24. 24. Read a Book
    25. 25. What is the DLR? <ul><li>The DLR is a layer on top of the CLR </li></ul><ul><ul><li>Designed for dynamic languages </li></ul></ul><ul><ul><li>Enables interop with running CLR programs and .NET libraries </li></ul></ul><ul><li>Compatible Implementations </li></ul><ul><ul><li>Python, Ruby, Javascript, and Scheme </li></ul></ul><ul><li>It’s Fast </li></ul><ul><ul><li>IronPython is about 2x the speed of Python </li></ul></ul><ul><li>The Languages are Open Source </li></ul><ul><ul><li>Community maintained and not directly supported </li></ul></ul>
    26. 26. The DLR is great for… <ul><li>Test Driven Development </li></ul><ul><li>Web Development </li></ul><ul><li>Using Code from Existing Dynamic Programs </li></ul><ul><li>Writing your own Language </li></ul><ul><li>Embedding in Applications </li></ul>
    27. 27. Dynamic in C#
    28. 28. DLR Architecture
    29. 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
    30. 30. Silverlight and the DLR
    31. 31. Embedded Languages in Your App
    32. 32. DLR Books and Resources http://www.silverlight.net/learn/dynamic-languages/ http://is.gd/DLRFAQ
    33. 33. Thank You <ul><li>Slides and code will be available on RichardMinerich.com later today. </li></ul><ul><li>[email_address] </li></ul>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.