Skip to content

Instantly share code, notes, and snippets.

@cartermp
Created June 13, 2016 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cartermp/154d89593731ca8b650154dd55f222f7 to your computer and use it in GitHub Desktop.
Save cartermp/154d89593731ca8b650154dd55f222f7 to your computer and use it in GitHub Desktop.
type BST<`T> =
| Empty // handle empty case
| Node of `T * BST<`T> * BST<`T> // Node with a value and two subtrees
let foo tree =
match tree with
| Empty -> // do something with empty case
| Node(value, left, right) -> // 'value' is the value, 'left' and 'right' are the subtrees
module Parsing =
open System
let tryParseHelper f s =
match (f s) with
| true, v -> Some v
| false, _ -> None
let tryParseDateTime s = tryParseHelper DateTime.Parse s
// add another if you want
open Parsing
let res = tryParseDateTime "2015-09-28"
// need to pattern match to actually get value, thus account for parse failure at compile time
match res with
| Some v -> // something with the value
| None -> // handle parse error
open System.Text.RegularExpressions
// active pattern, check this shit out
let (|Matches|_|) str pattern =
let mt = let mat = Regex.Match(str, pattern, RegexOptions.Compiled ||| RegexOptions.IgnoreCase)
if mt.Success then Some(str) else None
let fooRegex str pattern =
match str with
| Matches pattern -> // yay, we got a regex match
| _ -> // no match
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment