Functional Programming Concepts First-Class Functions (and Events) Strong Type Inference Immutability Make side-effects explicit (the M-word) Functional Reactive Programming Easy Concurrency Actor-model Messaging (similar to Erlang) Lazy Evaluation
Basic Syntax
let Similar to var in C#.
Tuples Type-safe collection of values C# out parameters are returned as tuples
Class Declaration
Abstract Classes
Interfaces
Inheritance
Records But then, why bother?
Discriminated Unions Functional inheritance
Pattern Matching
Extension Methods
Object Expressions
Control Flow with Pipes
Function Composition
Computation Workflows
Quotations This is supposed to be a practical discussion! Quotations are similar to LINQ Expressions.
REPL
Visual Studio Integration
Why Consider F#? You can do everything you did in C# within F# Some things are simpler, some harder Simpler: many OO design patterns Simpler: Asynchronous tasks Harder: GUI programming Terrific for specialized tasks (polyglot) Many great F# libraries Terse language = fewer lines of code = $ saved
When to use F# Asynchronous operations Concurrent operations Messaging (MailboxProcessor) Transformation operations (like SSIS) Computation-intensive operations (like SSAS) Creating Domain Specific Languages (DSLs) Aspect-Oriented Programming (AOP) Testing
FP vs. OOP
#1 Reason to use F# Your entire application is already just a series of LINQ statements!
Testing FsTest FsUnit
Generate Test Data
Mathematical Calculations Project Euler Problem 4: Project Euler Problem 3: Project Euler Problem 6
Excel Financial Functions
Parsers FSharp.PowerPack has a lexer and parser Fparsec Cashel
Automated Builds with Fake
Async Workflows
Async Workflows from C#
Functional AOP?
When not to use F# Just want to use the newest thing GUI programming You can do it Can’t use the designers Can still be a great tool When you are already proficient in C# and are focused almost entirely on OOP. Stick to what you know. You’ll be faster.
Summary “Write tomorrow’s legacy code today with C#” Chris Smith’s F# Facts Try F# Fall in love with F# Use F# The End
Resources F# is still CTP Try it in VS2010 Beta 1 Or use the VS2008 CTP installer Read books Foundations of F#(Pickering) Expert F# (Syme) Functional Programming for the Real World (Petricek and Skeet) F# for Scientists (Harrop)
Monads Monads are about function composition. Take a monoid:f(x) -> xg(x) -> xf(g(x)) -> x f.g x -> x Add a transformation:f(x) -> Mxg(x) -> Mxf(g(x)) -> Mx f.g x -> Mx
Creating Monadic Builders The parts of a monad:(in F#, this would be a workflow builder) x.Let (value, transform): sets a value, like LINQ’s Select(). x.Bind (value, transform): splits the value from the transform and passes it into the next function, like LINQ’s SelectMany(). x.Return (value): returns the result. x.Delay (transform): kicks off the workflow.
Computation workflow structure Typical structure of a workflow builder:type WorkflowBuilder () = member x.Bind (value, transform) = transform value member x.Return (value) = fun () -> value member x.Delay (transform) = fun () -> transform ()let workflow = new WorkflowBuilder() Execute like so:let myWorkflow = workflow { let x = 1 // x.Let let! y = 2 // x.Bind return x + y // x.Return }let result = myWorkflow () // x.Delay
F# is the new kid on the block, and many are wondering why they should care. F# offers a lot of really elegant solutions to some tasks many avoid simply because of the pain involved. What many don't realize is that F# was the basis for a lot of time-saving tools now available in C#, such as LINQ. That's not to say everyone should switch all their projects from VB.NET and C# to F#. Find out how you can start leveraging F# in your current projects by using the right language for the right job. less
0 comments
Post a comment