1. Async and Parallel Programming in F# Matthew Podwysocki Senior Consultant http://codebetter.com/ @mattpodwysocki
2. Agenda F# in 15 Minutes Why is concurrent programming so hard? What can F# do to help?
3. ...a functional, object-oriented, imperative and explorative programming language for .NET with influences from OCaml, C#, Haskell and Erlang Hi! I’m F#
4.
5. F# in 15 Minutes – The Facts F# is a general purpose .NETlanguage F# is a multi-paradigm language F# is a statically-typed language
6. F# in 15 Minutes - The Syntax binding names to values let lets = "Hello World" let (x, y) = (45, 54) let answer = x + y let numbers = [1 .. 10] let odds = [1; 3; 5; 7; 9] let square x = x * x let squareOf4 = square 4
7. F# in 15 Minutes - The Syntax functions as values fun let square x = x * x let squares = List.map (fun x -> x * x) [1..10] let squares = List.map square [1..10] Operators are functions too!
8. F# in 15 Minutes – The Syntax |> bringing order to chaos let (|>) x f = f x letsumOfSquares = List.sum (List.map square [1..10]) letsumOfSquares = [1..10] |> List.map square |> List.sum <| >> << See also:
9. F# in 15 Minutes – The Syntax discriminated unions type type Suit = | Spade | Heart | Club | Diamond type Rank = | Ace | King | Queen | Jack | Value ofint type Card = Card of Suit * Rank Microsoft Confidential
10. F# in 15 Minutes – The Syntax pattern matching match letcardValue (Card(s,r)) = match r with | Ace -> 11 | King | Queen | Jack -> 10 | Value(x) -> x let (x, y) = ("x", "y") let [a; b] = [1 ; 2] Microsoft Confidential
12. Shared State Gives Us… Race conditions! Obscure error messages! Late night debugging! Locks, mutexes and semaphores, oh my! Microsoft Confidential
13. How Can F# Help Us? Granularity Purity Immutability Libraries
14. In Praise of Immutability Immutable objects ... can be relied upon ... can transfer between threads ... can be aliased safely ... lead to (different) optimization opportunities
40. Resources - Blogs Don Symehttp://blogs.msdn.com/dsyme/ Luke Hobanhttp://blogs.msdn.com/lukeh/ Brian McNamarahttp://lorgonblog.spaces.live.com/ Chris Smithhttp://blogs.msdn.com/chrsmith/ Jomo Fisherhttp://blogs.msdn.com/jomo_fisher/ Planet F#http://feeds.feedburner.com/planet_fsharp
Editor's Notes
What is the Problem?Multithreaded programming is hard todayDoable by only a subgroup of senior specialistsParallel patterns are not prevalent, well known, nor easy to implementSo many potential problemsRaces, deadlocks, livelocks, lock convoys, cache coherency overheads, lost event notifications, broken serializability, priority inversion, and so on…Businesses have little desire to go deepBest developers should focus on business value, not concurrencyNeed simple ways to allow all developers to write concurrent code