Post on 11-Aug-2015
“First, it’s almost certainly true that functional programming is the next big thing. “
Robert C. Martin
“Object oriented programming makes code understandable by encapsulating moving parts. Functional programming makes code understandable by minimizing moving parts.“
Michael Feathers
“F# is excellent at concisely expressing business and domain logic.Developers trying to achieve explicit business logic within an Application may opt to express their domain in F# with the majority of plumbing code in C#.”
Characteristic Imperative approach Functional approach
Programmer focusHow to perform tasks (algorithms)
how to track changes in state.
What information is desired and what transformations are required.
State changes Important. Non-existent.
Order of execution Important. Low importance.
Primary flow controlLoops, conditionals, and function
(method) calls.Function calls, including recursion.
Primary manipulation unit
Instances of structures or classes.Functions as first-class objects
data collections.
Characteristic C# F#
Programmer focusHow to perform tasks (algorithms)
how to track changes in state.
What information is desired and what transformations are required.
State changes Important. Non-existent.
Order of execution Important. Low importance.
Primary flow controlLoops, conditionals, and function
(method) calls.Function calls, including recursion.
Primary manipulation unit
Instances of structures or classes.Functions as first-class objects
data collections.
Why F# ?
• Functional-first
• Very strong type inference
• Integrate with other .NET technologies
• Windows, Web, Xamarin iOS & Android, Linux
The functional language in .NET
public static class Calc {
public static decimal Square(decimal n){
return n * n;}
public static decimal MultiplyByPi(decimal n) {
return n*3.14m;}
public static decimal AreaOfCicle(decimal r){
return MultiplyByPi(Square(r));}
}
module Calclet square n = n ** 2.0
let multiplyByPi n = n * 3.14
let areaOfCircle r = multiplyByPi (square (r))
let square n = n ** 2.0let multiplyByPi n = n * 3.14
let areaOfCircle r = r |> square |> multiplyByPi
Pattern matching
let x = 9match x with
| 1 -> printfn “the number is one”| num when num < 10 ->
printfn “number is below 10”| _ -> printfn “number is above 10”
Measures
[<Measure>] type m[<Measure>] type s
let distance = 10.0<m>let time = 2.0<s>
let speed = distance / time
match speed with | x when x < 5.<m/s> -> printfn "Slow" | _ -> printfn "Fast"
Why F#?
• Compact, concise and readable syntax
• Clearity, also readable by domain experts
• Very good for modelling domain entities & value objects
• Perfect match for processing sets of data
• Async made easy through immutability
More interesting stuff
Lists vs SequencesType providers
My other session : “Domain Driven Design with F#”, Kilimanjaro 11.30
Rick’s session : “C#: Raise the bar with Functional & Immutable constructs”Pacific, 12.15
Your feedback is important!Scan the QR Code and let us know via the TechDays App.
Laat ons weten wat u van de sessie vindt!Scan the QR Code via de TechDays App.
Bent u al lid van de Microsot Virtual Academy?! Op MVA kunt u altijd iets nieuws leren over de laatste technologie van Microsoft. Meld u vandaag aan op de MVA Stand. MVA biedt 7/24 gratis online training on-demand voor IT-Professionals en Ontwikkelaars.