Created
June 13, 2016 21:56
-
-
Save cartermp/154d89593731ca8b650154dd55f222f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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