module Demo
let aBoolean = true
let someInt = 5
let someFloat = 4.3
let someString = "This is a string value"
let ( someOtherInt : int ) = 42
function addNumbers(first, second) {
return first + second;
}
addNumbers(3, 4);
let addNumbers first second =
first + second
addNumbers 3 4
let addNumbers first second =
first + second
addNumbers 3 4
module Demo
let addNumbers first second =
first + second
let addFive =
addNumbers 5
addFive 10
// 15
module Demo
let result = System.Math.Sin 1.0
open System.Math
let result2 = Sin 1.0
let randomGenerator = System.Random()
let randomValue = randomGenerator.Next()
module Demo
let tupleValue = (5, "a string")
let printTuple (tuplevalue : int * string) =
let (firstPart, secondPart) = tupleValue
printfn "The number is %d, the string is '%s'" firstPart secondPart
printTuple tuple
module Demo
let tupleValue = (5, "a string")
let printTuple (firstPart : int, secondPart : string) =
printfn "The number is %d, the string is '%s'" firstPart secondPart
printTuple tuple
module Demo
type Person =
{ FirstName : string
LastName : string
YearOfBirth : int }
let batman =
{ FirstName = "Bruce"
LastName = "Wayne"
YearOfBirth = 1939
}
let printPerson (person : Person) =
printfn "%s %s (Born %d)" person.FirstName person.LastName
person.YearOfBirth
printPerson batman
// Bruce Wayne (Born 1939)
module Demo
let batman =
{ FirstName = "Bruce"
LastName = "Wayne"
YearOfBirth = 1939
}
let theDarkKnight =
{ batman with YearOfBirth = 1986 }
printPerson batman
// Bruce Wayne (Born 1939)
printPerson theDarkKnight
// Bruce Wayne (Born 1986)
module Demo
type Direction =
| Up
| Right
| Left
| Down
let printDirection (dir : Direction) =
match dir with
| Up -> printfn "Up"
| Right -> printfn "Right"
| Left -> printfn "Left"
| Down -> printfn "Down"
printDirection Up
module Demo
type Status<'successType> =
| Pending
| Running of current : int * total : int
| Success of 'successType
| Error of string
let printStatus status =
match status with
| Pending -> printfn "Pending"
| Running(0, n) -> printfn "Starting with first item of %d" n
| Running(i, n) -> printfn "Processing %d of %d" i n
| Success success ->
printfn "Succesfully completed, result is %O" success
| Error msg -> printfn "Error: %s" msg
printStatus (Success 42)
// Succesfully completed, result is 42
printStatus (Error "unknown problem")
// Error: unknown problem
let printStatus status =
match status with
| Pending -> printfn "Pending"
| Running(0, n) -> printfn "Starting with first item of %d" n
| Running(i, n) -> printfn "Processing %d of %d" i n
| Success success ->
printfn "Succesfully completed, result is %O" success
| Error msg -> printfn "Error: %s" msg
printStatus (Success 42)
// Succesfully completed, result is 42
printStatus (Error "unknown problem")
// Error: unknown problem
printStatus (Success 42)
// Succesfully completed, result is 42
printStatus (Error "unknown problem")
// Error: unknown problem
module Demo
// Defined in the standard library
type Option<'a> =
| None
| Some of 'a
let doSomething (optionalInteger : Option<int>) =
match optionalInteger with
| Some value -> printfn "Got %d" value
| None -> printfn "Didn't have a value!"
module Demo
let doSomething (person: Person) =
match person with
| null -> printfn "Didn't have a value!"
| personValue -> printfn "Got %O" personValue
module Demo
type Result<'success, 'error> =
| Ok of 'success
| Error of 'error
let doSomething (intResult : Result<int, string>) =
match intResult with
| Ok value -> printfn "Got %d" value
| Error err -> printfn "Got an error: %s" err
module Demo
try
someExceptionThrowingFunction()
with
| :? IOException as ex -> printfn "IOException was thrown: %O!" ex
| _ -> printfn "Some other exception was thrown!"
module Demo
use processInstance = new Process()
processInstance.StartInfo.FileName <- "echo"
processInstance.StartInfo.CreateNoWindow <- true
processInstance.Start();
module Demo
let createPerson (context : HttpContext) : Option<Person> =
let firstNameOption = context.TryGetQueryParam "firstname"
let lastNameOption = context.TryGetQueryParam "lastname"
let yearBornOption = context.TryGetQueryParam "yearBorn"
match firstNameOption, lastNameOption, yearBornOption with
| Some firstName, Some lastName, Some yearBorn ->
Some { FirstName = firstName
LastName = lastName
YearOfBirth = yearBorn }
| _ -> None
module Demo
let createPerson (context : HttpContext) : Option<Person> =
option {
let! firstNameOption = context.TryGetQueryParam "firstname"
let! lastNameOption = context.TryGetQueryParam "lastname"
let! yearBornOption = context.TryGetQueryParam "yearBorn"
return { FirstName = firstName
LastName = lastName
YearOfBirth = yearBorn }
}
module Demo
let retrieveFromRemoteStore key : Async<string> =
// ommited
let createPerson (context : HttpContext) : Async<Person> =
async {
let! firstName = retrieveFromRemoteStore "firstname"
let! lastName = retrieveFromRemoteStore "lastname"
let! yearBorn = retrieveFromRemoteStore "yearBorn"
return { FirstName = firstName
LastName = lastName
YearOfBirth = yearBorn }
}
module Demo
[<Measure>] type m
[<Measure>] type sec
[<Measure>] type kg
let distance = 1.0<m>
let time = 2.0<sec>
let speed = distance/time
let acceleration = speed/time
let mass = 5.0<kg>
let force = mass * speed/time
module Demo
let msft = Stocks.Load("http://www.google.com/finance/historical?q=MSFT&output=csv")
// Look at the most recent row. Note the 'Date' property
// is of type 'DateTime' and 'Open' has a type 'decimal'
let firstRow = msft.Rows |> Seq.head
let lastDate = firstRow.Date
let lastOpen = firstRow.Open
// Print the prices in the HLOC format
for row in msft.Rows do
printfn "HLOC: (%A, %A, %A, %A)" row.High row.Low row.Open row.Close
The positives
The negatives