Skip to content

Instantly share code, notes, and snippets.

View BuildingsDrawer.fs
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
View Perimeter.fs
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 Sep 22, 2017
Discriminated Unions demo - Monopoly example
View DiscriminatedUnions.fs
#if COMPILED
namespace CasualFSharp
#endif
module Monopoly =
open System
type Player =
| Boot
@misterspeedy
misterspeedy / PatternMatching.fs
Created Sep 12, 2017
Source code for "Casual F# - Pattern Matching"
View PatternMatching.fs
namespace CasualFSharp
module PatternMatching =
let describeInt i =
match i with
| 1 -> "One"
| 2 -> "Two"
| _ -> "Many"
@misterspeedy
misterspeedy / gist:6963358
Created Oct 13, 2013
Cumulative binomial distribution probability
View gist:6963358
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 Oct 8, 2013
Using a Hand argument prevents index out of range in places like this.
View gist:6890444
if (hand.Cards.[0].Rank) = Ace && (Consecutive hand.Cards) && (SameSuit hand.Cards) then
@misterspeedy
misterspeedy / gist:6890413
Created Oct 8, 2013
Outcome of comparing two hands
View gist:6890413
/// 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
View gist:6890374
/// 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 Oct 8, 2013
Partial active pattern for Four of a Kind
View gist:6890361
let (|FourOfAKind|_|) (hand : Hand) =
if (RankCounts hand.Cards).[0] = 4 then
FourOfAKind |> Some
else
None
@misterspeedy
misterspeedy / gist:6890354
Created Oct 8, 2013
Partial active pattern for Straight Flush
View gist:6890354
let (|StraightFlush|_|) (hand : Hand) =
if (SameSuit hand.Cards) && (Consecutive hand.Cards) then
StraightFlush |> Some
else
None