Introduction to F# for the C# developer

41
F# for the C# developer Nate Peterson @njpetersonpa

Transcript of Introduction to F# for the C# developer

Page 1: Introduction to F# for the C# developer

F# for the

C# developer

Nate Peterson@njpetersonpa

Page 2: Introduction to F# for the C# developer

Brief intro to F#Thinking functionallyLook at some code

Page 3: Introduction to F# for the C# developer

Simple vs. Complex

and

Familiar vs. Unfamiliar

Page 4: Introduction to F# for the C# developer

What’s NOT

in this talk

Page 5: Introduction to F# for the C# developer

What is F#?

Page 6: Introduction to F# for the C# developer

Functional-first language

Page 7: Introduction to F# for the C# developer

Strongly typed

Page 8: Introduction to F# for the C# developer

let add x y = x + y

add 1 1

Page 9: Introduction to F# for the C# developer

F# is more than just a new language

It’s a new way of thinking

Page 10: Introduction to F# for the C# developer

Immutability

Page 11: Introduction to F# for the C# developer

Side-effect free functions

Page 12: Introduction to F# for the C# developer

Functions are first class

Page 13: Introduction to F# for the C# developer

let add x y = x + y

let add42 x = add 42 x

add42 1

Page 14: Introduction to F# for the C# developer

let add x y = x + y

let add42 = add 42

add42 1

Page 15: Introduction to F# for the C# developer

Sum of Squares- C#

Page 16: Introduction to F# for the C# developer

public static class SumOfSquaresHelper { public static int Square(int i) { return i * i; }

public static int SumOfSquares(int n) { int sum = 0; for (int i = 1; i <= n; i++) { sum += Square(i); } return sum; } }

Page 17: Introduction to F# for the C# developer

Sum of Squares- C# (again)

Page 18: Introduction to F# for the C# developer

public static class FunctionalSumOfSquaresHelper { public static int SumOfSquares(int n) { return Enumerable.Range(1, n) .Select(i => i * i) .Sum(); } }

Page 19: Introduction to F# for the C# developer

Sum of Squares- F#

Page 20: Introduction to F# for the C# developer

let square x = x * x

let sumOfSquares n = [1..n] |> List.map square|> List.sum

sumOfSquares 100

Page 21: Introduction to F# for the C# developer

Think about…Concise – no noiseSimple vs. Complex

Familiar vs. Unfamiliar

where are the typeswhere’s the {}

Page 22: Introduction to F# for the C# developer

Sum of Squares (evens only)

Page 23: Introduction to F# for the C# developer

public static class SumOfSquaresHelper { public static int Square(int i) { return i * i; }

public static int SumOfSquares(int n) { int sum = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 0) { sum += Square(i); } } return sum; } }

Page 24: Introduction to F# for the C# developer

public static class FunctionalSumOfSquaresHelper { public static int SumOfSquares(int n) { return Enumerable.Range(1, n) .Select(i => i * i) .Where(i => i % 2 == 0) .Sum(); } }

Page 25: Introduction to F# for the C# developer

let square x = x * x

let isEven x = x % 2 = 0

let sumOfSquares n = [1..n] |> List.filter List.map square|> List.filter isEven |> List.sum

sumOfSquares 100

Page 26: Introduction to F# for the C# developer

FizzBuzz - C#

Page 27: Introduction to F# for the C# developer

public List<string> Create() { var results = new List<string>(); for (var i = 0; i < 100; i++) { results.Add(FizzBuzz(i)); }

return results; }

private static string FizzBuzz(int num) { if (num % 3 == 0 && num % 5 == 0) { return "FizzBuzz"; }

if (num % 3 == 0) { return "Fizz"; }

if (num % 5 == 0) { return "Buzz"; }

return num.ToString(); }

Page 28: Introduction to F# for the C# developer

FizzBuzz - C# (again)

Page 29: Introduction to F# for the C# developer

public List<string> Create(IEnumerable<int> sequence) { return Enumerable.Range(1, 100).Select(FizzBuzz).ToList(); } private static string FizzBuzz(int num) { if (num % 3 == 0 && num % 5 == 0) { return "FizzBuzz"; }

if (num % 3 == 0) { return "Fizz"; }

if (num % 5 == 0) { return "Buzz"; }

return num.ToString(); }

Page 30: Introduction to F# for the C# developer

FizzBuzz - F#

Page 31: Introduction to F# for the C# developer

let FizzBuzz number = match number % 3, number % 5 with | 0, 0 -> "FizzBuzz" | 0, _ -> "Fizz" | _, 0 -> "Buzz" | _ -> string number

[1..100]|> List.map FizzBuzz|> List.reduce (sprintf "%s\r\n%s")

Page 32: Introduction to F# for the C# developer

Think about…Concise – again

Pattern matching for flow control

Page 33: Introduction to F# for the C# developer

Immutable Person – C#

Page 34: Introduction to F# for the C# developer

public class Person { public Person(string firstName, string lastName) { } }

Page 35: Introduction to F# for the C# developer

public class Person { private string _firstName; private string _lastName;

public Person(string firstName, string lastName) { _firstName = firstName; _lastName = lastName; } }

Page 36: Introduction to F# for the C# developer

public class Person { private string _firstName; private string _lastName;

public Person(string firstName, string lastName) { _firstName = firstName; _lastName = lastName; }

public string FirstName { get { return _firstName; } } }

Page 37: Introduction to F# for the C# developer

public class Person { private string _firstName; private string _lastName;

public Person(string firstName, string lastName) { _firstName = firstName; _lastName = lastName; }

public string FirstName { get { return _firstName; } }

public string LastName { get { return _lastName; } } }

Page 38: Introduction to F# for the C# developer

public class Person { private readonly string _firstName; private readonly string _lastName;

public Person(string firstName, string lastName) { _firstName = firstName; _lastName = lastName; }

public string FirstName { get { return _firstName; } }

public string LastName { get { return _lastName; } } }

Page 39: Introduction to F# for the C# developer

Immutable Person – F#

Page 40: Introduction to F# for the C# developer

type Person = {FirstName:string; LastName:string}

let nate = {FirstName=“Nate"; LastName=“Peterson"}

Page 41: Introduction to F# for the C# developer

Nate Peterson@njpetersonpa