Skip to content

Instantly share code, notes, and snippets.

namespace Buildings
// Needs Easy GIS from Nuget: https://www.nuget.org/packages/Huitian.EGIS.ShapeFileLib/
// Also need to add a reference to System.Drawing
module Drawer =
open System
open System.Drawing
namespace OSBuildings
module Perimeter =
// Need to include this Nuget package: https://www.nuget.org/packages/Huitian.EGIS.ShapeFileLib/
// Need to add a reference to System.Drawing
let shpPath = @"C:\Data\OSBuildings\wales_buildings_clipped.shp"
let demo1() =
@misterspeedy
misterspeedy / DiscriminatedUnions.fs
Created September 22, 2017 20:58
Discriminated Unions demo - Monopoly example
#if COMPILED
namespace CasualFSharp
#endif
module Monopoly =
open System
type Player =
| Boot
@misterspeedy
misterspeedy / PatternMatching.fs
Created September 12, 2017 19:59
Source code for "Casual F# - Pattern Matching"
namespace CasualFSharp
module PatternMatching =
let describeInt i =
match i with
| 1 -> "One"
| 2 -> "Two"
| _ -> "Many"
@misterspeedy
misterspeedy / gist:6963358
Created October 13, 2013 15:07
Cumulative binomial distribution probability
let CumulativeDistribution (x : int) (n : int) (p : float) =
let EssentiallyZero = 10E-12
let m = (float(n) * p) |> truncate |> int
let CalcCurrent value k =
if k > m then
value * float(n - k + 1) * p / (float(k) * (1. - p))
else
value * float(k + 1) * (1. - p) / (float(n - k) * p)
@misterspeedy
misterspeedy / gist:6890444
Created October 8, 2013 19:46
Using a Hand argument prevents index out of range in places like this.
if (hand.Cards.[0].Rank) = Ace && (Consecutive hand.Cards) && (SameSuit hand.Cards) then
@misterspeedy
misterspeedy / gist:6890413
Created October 8, 2013 19:45
Outcome of comparing two hands
/// The possible outcomes when comparing two Poker hands.
type Outcome = Win | Draw | Lose
with
static member FromRanks (rank1 : Rank) (rank2 : Rank) =
let sortValue1, sortValue2 = rank1.SortValue, rank2.SortValue
if sortValue1 > sortValue2 then
Win
elif sortValue1 < sortValue2 then
Lose
else
@misterspeedy
misterspeedy / gist:6890374
Created October 8, 2013 19:43
Comparing two hands
/// Compare two hands, the first of which can Win, Draw or Lose against the second.
let CompareHands (hand1 : Hand) (hand2 : Hand) =
match hand1, hand2 with
| RoyalFlush, RoyalFlush ->
raise (ArgumentException("Impossible combination: two Royal Flushes"))
| RoyalFlush, _ -> Win
| _, RoyalFlush -> Lose
| StraightFlush, StraightFlush ->
@misterspeedy
misterspeedy / gist:6890361
Created October 8, 2013 19:42
Partial active pattern for Four of a Kind
let (|FourOfAKind|_|) (hand : Hand) =
if (RankCounts hand.Cards).[0] = 4 then
FourOfAKind |> Some
else
None
@misterspeedy
misterspeedy / gist:6890354
Created October 8, 2013 19:41
Partial active pattern for Straight Flush
let (|StraightFlush|_|) (hand : Hand) =
if (SameSuit hand.Cards) && (Consecutive hand.Cards) then
StraightFlush |> Some
else
None