Skip to content

Instantly share code, notes, and snippets.

@dreadedsoftware
Created May 3, 2016 01:32
Show Gist options
  • Save dreadedsoftware/06422e4f811876eb1d71c92cbca49754 to your computer and use it in GitHub Desktop.
Save dreadedsoftware/06422e4f811876eb1d71c92cbca49754 to your computer and use it in GitHub Desktop.
module PatternMatch
type Type =
|Something of string * int
|Nothing
type Case =
|CaseA of a:string * b:int
|CaseB of a:string * b:double
let (|A|B|C|NOPE|) (input:char) =
match input with
|'A' -> A
|'B' -> B
|'C' -> C
|_ -> NOPE
let check() =
let rand = System.Random()
let item = rand.NextDouble()
let du =
match item with
|under when 0.5 < under -> Nothing
|over ->
Something(rand.Next().ToString(), rand.Next())
let result =
match du with
|Something(s, i) -> s + i.ToString()
|Nothing -> "Nothing"
printfn "%A" result
let cc =
match item with
|under when 0.5 < under -> CaseA("", 0)
|over -> CaseB(over.ToString(), rand.NextDouble())
let result2 =
match cc with
|CaseA("", 0) -> "Nothing"
|CaseB(a, b) -> a + " " + b.ToString()
|_ -> "Should not happen"
printfn "%A" result2
let toThree = rand.Next(4)
let myChar =
match toThree with
|1 -> 'A'
|2 -> 'B'
|3 -> 'C'
|_ -> '-'
let result3 =
match myChar with
|A -> "Got A"
|B -> "B is here"
|C -> "And C wins"
|NOPE -> "Nothing"
printfn "%s" result3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment