Download - Let the |> fun begin
![Page 1: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/1.jpg)
Let the |> fun beginAn introduction to F# (part 2)
Bogdan Brinzarea-IamandiBanca Romaneasca
25 February 2010
![Page 2: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/2.jpg)
AgendaTopic Covered
TodayHistory 22 february
2010From imperative to functional 22 february
2010Fundamentals 22 february
2010Data structures 22 february
2010Pattern Matching 22 february
2010Immutability vs. Mutability 22 february
2010Object Oriented Programming 22 february
2010Async and Parallel Programming
Unit testing
![Page 3: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/3.jpg)
Asynchronous and parallel programming
![Page 4: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/4.jpg)
Why should these matter to me?Problem SolutionI/O bound application Asynchronous processingCPU bound application Parallel and distributed
processingFreezing UI Asynchronous processingSharing state Immutability
![Page 5: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/5.jpg)
Why should these matter to me? Working with threads is difficult Synchronizing and sharing state are
difficult Introducing bugs is easy The code is complicated
![Page 6: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/6.jpg)
Parallel and reactive language Multiple active evaluations (threads
computing results) Multiple pending reactions (callbacks
and agents waiting for events and messages)
![Page 7: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/7.jpg)
Background worker pattern Not useful for asynchronous
operations Imperative programming Sharing mutable data between
threads Cancellations Raising events in the right thread
![Page 8: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/8.jpg)
Immutability
Optimization opportunities Easily transferable between threads Reliability
![Page 9: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/9.jpg)
Async advantages
Ease of change Cancellation checking Simple resource management Exception propagation
![Page 10: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/10.jpg)
async {…}
Creates async objects These tasks can be run
In the current thread In a background thread In parallel using fork/join mechanism As continuations
![Page 11: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/11.jpg)
Async.Parallel
Takes async objects and creates async tasks
Uses QueueUserWorkItem Fork/join patternDoes not report progress
seq<Async<'T>> -> Async<'T Array>
![Page 12: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/12.jpg)
Async.RunSynchronously Runs synchronous computation Waits for the result Batch processing jobs Matrix multiplication
![Page 13: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/13.jpg)
Async.StartWithContinuations Starts and ends in the same thread Useful for UI updating Continuations for:
Complete Exception Cancellation
![Page 14: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/14.jpg)
! = async
let! for async method calls The thread is suspended until the
result in available The rest of the code runs as a
continuation use! resource disposing equivalent
of let!
![Page 15: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/15.jpg)
Parallel programming
Leverage parallel hardware capabilities
Data parallel programming with PLinq Easy to implement Abstracts away complexity Transparent partition and merge
operations Works on seq<a> and IEnumerable<T>
Task parallel programming using the new Task Parallel Library in .NET 4.0
![Page 16: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/16.jpg)
Agent model concurrency Erlang message passing style “An actor is a computational entity that, in
response to a message it receives, can concurrently:▪ send a finite number of messages to
other actors;▪ create a finite number of new actors;▪ designate the behavior to be used for
the next message it receives.”
![Page 17: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/17.jpg)
Agent model concurrency Asynchronous message passing Can have even 1000 agents Agents are lightweight Based on async programming State isolation between agents No concurrency and data races
![Page 18: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/18.jpg)
Unit testing
![Page 19: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/19.jpg)
Object oriented approach NUnit xUnit.net
![Page 20: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/20.jpg)
Functional approach
FsUnit based on NUnit FsCheck inspired from Haskell’s
QuickCheck FsTest based on xUnit.net NaturalSpec based on NUnit FsSpec readable DSL
![Page 21: Let the |> fun begin](https://reader036.vdocument.in/reader036/viewer/2022062410/5681657b550346895dd81096/html5/thumbnails/21.jpg)
Resources Expert F# by Don Syme Programming F# by Chris Smith CTO Corner - http://ctocorner.com/fsharp/book/ HubFS
http://cs.hubfs.net/ Matthew Podwysocki
http://weblogs.asp.net/podwysocki/ Don Syme
http://blogs.msdn.com/dsyme Chris Smith
http://blogs.msdn.com/chrsmith/