techdaysnl 2015 - f# for c# developers

35

Upload: ronald-harmsen

Post on 11-Aug-2015

279 views

Category:

Software


0 download

TRANSCRIPT

F# for C# developersRonald Harmsen

Why?

“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#.”

Functional programming

• Declarative

• Clearity

• Composability

• Immutability

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

FunctionsEverything is a function

let a = 10

int a () {return

10;}

let sum a b = a + b

int sum (int a, int b) {return a + b;

}

Composability

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

let square n = n ** 2.0let multiplyByPi n = n * 3.14

let areaOfCircle r = square >> multiplyByPi

Types: tuples

type person = name * age

Types: record

type person = { name: string; age : int

}

Types: discriminated unions

type Employee =| Developer| Manager

Types: discriminated unions

type Employee =| Developer of Person| Manager of Person

DEMOTypes in F#

Pattern matching

let data = (“Den Haag”, 070)let city, areanumber = data

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”

Option

NULL -> NullReferenceException

Option

type Option<'a> = | Some of 'a | None

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

Demo

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

Ronald [email protected]@ronaldharmsen

Simple. Clear. Software.

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.