Story – Option Types – Pattern Matching



Story – Option Types – Pattern Matching

0 0


IntroToFSharpSlides

Slides for an Introduction to F# presentation.

On Github jwood803 / IntroToFSharpSlides

A Gentle Intro to F#

Go over the main points of the talk - introducting F# as a language, why I think it's great, and how it can be incorporated.

Story

Start on the small story of how I got into F#.
let rec cast<'a> (myList: obj list) =
 match myList with
 | head::tail ->
  match head with
  | :? 'a as a -> a::(cast tail)
  | _ -> cast tail
 | [] -> []
						
Jonathan Wood jwood@wintellect.com / dotnetmeditations.com / @JWood Briefly go over who I am and Wintellect.

Why F#?

  • Fewer bugs
    • Immutable by default
    • Units of Measure
  • Type providers
  • Option types
  • Pattern matching
  • Rapid prototyping with the F# Interactive
  • Interoperability
Briefly mention each of these points. Most will be in detail later.

Demo - Basic Syntax

Go over the two console apps to briefly show syntax differences. Make sure to mention that whitespace DOES matter here.

Immutability

Demo in the FSI about immutability. Ask what `x = 2` would result in. Show what happens when assign to a value and how to make it mutable.

Option Types

let hasBalance (balance: float option) =
	match balance with
	| Some b -> printfn "Balance - %f" b
	| None -> printfn "No balance available"
						

Pattern Matching

let x = "4"

match Int32.TryParse(x) with
    | (true, value) -> printfn "Parsed - %i" value
    | (false, _) -> printfn "No parsing for you!"
						
Go over a few things in this code: - Mention tuples - Go over the pattern matching - Mention the `wildcard` character

Type Providers

open FSharp.Data

let deliveryData = CsvProvider<"delivery_data.csv">.GetSample()
						

Units of Measure

  • Add meta-data to numeric types
  • Adds extra type safety
[<Measure>] type dollar

let dollarsToEuros dollars: float<dollar> =
	dollars * 0.74<dollar>
Show this in the interactive as well as the error it can give when types don't match.

How to use F# day-to-day?

Go over a few projects to describe some uses for F#.

Keep in mind...

  • Tooling
  • Community

It is functional first

@JWood / jwood@wintellect.com