Introduction to F# for the C# developer
Transcript of Introduction to F# for the C# developer
![Page 1: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/1.jpg)
F# for the
C# developer
Nate Peterson@njpetersonpa
![Page 2: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/2.jpg)
Brief intro to F#Thinking functionallyLook at some code
![Page 3: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/3.jpg)
Simple vs. Complex
and
Familiar vs. Unfamiliar
![Page 4: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/4.jpg)
What’s NOT
in this talk
![Page 5: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/5.jpg)
What is F#?
![Page 6: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/6.jpg)
Functional-first language
![Page 7: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/7.jpg)
Strongly typed
![Page 8: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/8.jpg)
let add x y = x + y
add 1 1
![Page 9: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/9.jpg)
F# is more than just a new language
It’s a new way of thinking
![Page 10: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/10.jpg)
Immutability
![Page 11: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/11.jpg)
Side-effect free functions
![Page 12: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/12.jpg)
Functions are first class
![Page 13: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/13.jpg)
let add x y = x + y
let add42 x = add 42 x
add42 1
![Page 14: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/14.jpg)
let add x y = x + y
let add42 = add 42
add42 1
![Page 15: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/15.jpg)
Sum of Squares- C#
![Page 16: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/17.jpg)
Sum of Squares- C# (again)
![Page 18: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/19.jpg)
Sum of Squares- F#
![Page 20: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/21.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/22.jpg)
Sum of Squares (evens only)
![Page 23: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/23.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/24.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/25.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/26.jpg)
FizzBuzz - C#
![Page 27: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/27.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/28.jpg)
FizzBuzz - C# (again)
![Page 29: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/29.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/30.jpg)
FizzBuzz - F#
![Page 31: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/31.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/32.jpg)
Think about…Concise – again
Pattern matching for flow control
![Page 33: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/33.jpg)
Immutable Person – C#
![Page 34: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/34.jpg)
public class Person { public Person(string firstName, string lastName) { } }
![Page 35: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/35.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/36.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/37.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/38.jpg)
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](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/39.jpg)
Immutable Person – F#
![Page 40: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/40.jpg)
type Person = {FirstName:string; LastName:string}
let nate = {FirstName=“Nate"; LastName=“Peterson"}
![Page 41: Introduction to F# for the C# developer](https://reader034.fdocuments.us/reader034/viewer/2022042608/55c56fe2bb61eb12538b4736/html5/thumbnails/41.jpg)
Nate Peterson@njpetersonpa