Skip to content

Instantly share code, notes, and snippets.

@sebfia
Created June 28, 2014 16:44
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 sebfia/d386cadc036113616661 to your computer and use it in GitHub Desktop.
Save sebfia/d386cadc036113616661 to your computer and use it in GitHub Desktop.
let (|Int|_|) str =
match Int32.TryParse(str) with
| (true,int) -> Some int
| _ -> None
let (|Bool|_|) str =
match Boolean.TryParse(str) with
| (true,bool) -> Some bool
| _ -> None
let (|DeNull|_|) arg =
match arg with
| null -> None
| a -> Some a
let (|Date|_|) str =
match DateTime.TryParse(str) with
| (true,date) -> Some date
| _ -> None
let whatAmI =
function
| DeNull str ->
match str with
| Int _ -> printfn "I'm an Int"
| Bool _ -> printfn "I'm a Bool"
| Date _ -> printfn "I'm a DateTime"
| _ -> printfn "I'm something which I don't know, but definitely no dumb null"
| _ -> printfn "I'm a dumb null from another planet"
@sebfia
Copy link
Author

sebfia commented Jun 28, 2014

That's what I love F# and ActivePatterns for:
It makes my code readable and succinct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment