namespace System
namespace System.Net
namespace System.Net.Mail
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member GetSlice : startIndex:int option * endIndex:int option -> 'T list
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val reduce : reduction:('T -> 'T -> 'T) -> list:'T list -> 'T

Full name: Microsoft.FSharp.Collections.List.reduce
type 'T option = Option<'T>

Full name: Microsoft.FSharp.Core.option<_>
module Option

from Microsoft.FSharp.Core
union case Option.None: Option<'T>
union case Option.Some: Value: 'T -> Option<'T>
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
Multiple items
type MeasureAttribute =
  inherit Attribute
  new : unit -> MeasureAttribute

Full name: Microsoft.FSharp.Core.MeasureAttribute

--------------------
new : unit -> MeasureAttribute

…you have to say to yourself:

“I don’t know what I’m doing. We, as a field, don’t know what we’re doing. We don’t know what programming is, we don’t know what computing is, we don’t even know what a computer is.”

And once you truly understand that, once you truly believe that – then you’re free, and you can think anything.


Bret Victor, The Future of Programming

F# and
Functional Programming

How I got here

Why I care

  • Fun learning something new
  • Interesting technology from Microsoft
  • Usable in my day to day job
  • Makes my C# coding better

F#

  • ML (1974) / OCaml
  • Open source since 2005
  • Functional First but very pragmatic
  • Full interop with C#
  • Runs everywhere C# does + a few more

Send an email C#

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
using System.Net;
using System.Net.Mail;

public static class Emailer
{
  public static void SendTestEmail(string toEmail, string body)
  {
    var user = "dave.glassborow@myemail.co.uk";
    using (var msg = new MailMessage(user, toEmail))
    {
      msg.Subject = "Test";
      msg.Body = body;
      using(var client = new SmtpClient("smtp.office365.com",587))
      {
        client.EnableSsl = true;
        client.Credentials = new NetworkCredential(user, "****");
        client.Send(msg);
      }
    }
  }
}

Emailer.SendTestEmail( "dave@conceptfirst" "From Office365")

Send an email F#

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
open System.Net
open System.Net.Mail

/// Send email via office 365
let sendTestEmail toEmail body =
    let user = "dave.glassborow@myemail.co.uk"
    use msg = new MailMessage(user,toEmail)
    msg.Subject <- "Test"
    msg.Body <- body 
    use client = new SmtpClient("smtp.office365.com",587)
    client.EnableSsl <- true  
    client.Credentials <- NetworkCredential(user,"****")
    client.Send(msg)

//sendTestEmail "dave@conceptfirst" "From Office365"
  • Whitespace
  • Inference
  • Superset of C#

Functional programming

  • I have to think less, the compiler does more for me
  • Think SQL Select

Controlling complexity is the essence of computer programming.

Core values

  • Seperate data and processes
  • Transform into new data
  • Sane defaults
  • Reason about code
  • Composiblity: Lego
  • Declarative
  • Functions: first class, making them, changing them

There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.

Object Orientation

  • Object oriented programming makes code understandable by encapsulating moving parts. Functional programming makes code understandable by minimizing moving parts.

Object Orientation

The problem with object-oriented languages is they’ve got all this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.

  • Packets of hidden state, hard to reason about
  • Things can change behind your back, especially in multi-threading
  • Model with Subclassing, but not always worth the hassle ?

Advantages

  • Manage complexity
  • Less stressful to code
  • More powerful, program at a higher level
  • Think differently, apply it to other langauges
  • Pit of success
  • REPL

Disadvantages

  • More abstract
  • Mathematical naming
  • Different way of thinking
  • More thinking, less typing, which is harder
  • Rabbit hole: Haskell, Dependant types, Category Theory

F# features

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
open System
let simpleValue = 5
let upper (s:string) = s.ToUpper()
let fact      x      = List.reduce (fun total v -> v * total ) [1..x]
let factorial x      = List.reduce (*) [1..x]
let factorialPipe x  = [1..x] |> List.reduce (*)
type Person = {
    Name: string
    Dob: DateTime option
}

F# cheatsheet

Pipelining

C#

1: 
2: 
public int AddOne(string x) =>
   int.Parse(x.Trim()) + 1;

F#

1: 
2: 
let addOne(x:string) =
  x.Trim() |> Int32.Parse |> (+) 1

Union types

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
type Option<'t> =
| None
| Some of 't

let getName name = match name with
                   | None      -> "-Unknown-"
                   | Some name -> name

let person = Some "david"
getName person |> printfn "%s"

Union types

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
type Suit = Clubs | Spades | Hearts | Diamonds
type Rank = int

type Score =
| HighCard of Rank
| OnePair of Rank
| TwoPair of Higher:Rank * Lower:Rank
| ThreeOfKind of Rank
| Straight of High:Rank
| Flush
| FullHouse of Three:Rank * Two:Rank
| FourOfKind of Rank
| StraightFlush of High:Rank
| RoyalFlush

RoyalFlush > Flush

Union types

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
type Royal = Ace | King | Queen | Jack 

let value hand =
    match hand with
    | Jack  -> 11
    | Queen -> 12
    | King  -> 13
    | Ace   -> 14

Union types

1: 
2: 
3: 
4: 
5: 
type PaymentType =
| Cash
| Cheque
| CreditCard of Number:string * CCV:string
| Bitcoin of key:string

Record types

1: 
2: 
3: 
type Person = { Name: string }

{ Name = "David" } = { Name = "David" }

Units of measure

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
[<Measure>] type m
[<Measure>] type sec
[<Measure>] type kg

let distance = 1.0<m>
let time = 2.0<sec>
let speed = 2.0<m/sec>
let acceleration = 2.0<m/sec^2>
let force = 5.0<kg m/sec^2>
let travel = time * speed

Type Providers

  • Databases (ORMS, querys, sprocs)
  • Regular Expressions
  • AWS S3 / Azure storage
  • WMI
  • OData
  • Hadoop
  • Slack
  • R

REPL

Where to use F#

  • REPL
    • utilities
    • machine learning
    • exploring
  • Complex
    • complex logic and domains
    • async code
  • Functional
    • pipelines
    • parsers

Where NOT to use F#

  • Basic mvc
  • Xamarin

Applying FP outside of FP

Applying functional thinkings to other languages

F# -> C#

  • Generics
  • Nullable types
  • LINQ
  • Async/Await
  • Tuples
  • Pattern matching
  • Record types
  • Union types
  • C# with static functions and dtos
 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
public class Person
{
    public string Email { get; set; }
    public string Name {get; set; }
}

public static class Emailer
{
    public static void SendEmail(Person who)
    {
        // emailing code ...
    }
}

Javascript

Philosophy

  • Logic, Sets, Programming
  • Category theory
  • Discovered -or- the way we think?

Find out more

F#

Find out more

Functional Programming

Thought for the day

Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.