Skip to content

Instantly share code, notes, and snippets.

@jbrechtel
Created March 27, 2014 13:04
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 jbrechtel/9807126 to your computer and use it in GitHub Desktop.
Save jbrechtel/9807126 to your computer and use it in GitHub Desktop.
module James
open System
let strToInt str =
try
Some (Int32.Parse(str))
with
| _ -> None
type OptionBuilder () =
member this.Return x = Some(x)
member this.Bind(e, f) = match e with
| Some(x) -> f x
| None -> None
let myWorkflow = new OptionBuilder()
let stringAddition x y z =
myWorkflow
{
let! a = strToInt x
let! b = strToInt y
let! c = strToInt z
return a + b + c
}
stringAddition "3" "2" "1"
stringAddition "99" "abc" "1"
let strAdd str i =
match strToInt str with
| Some(x) -> Some(i + x)
| None -> None
let (>>=) m f = Option.bind f m
let y = strToInt "1" >>= strAdd "100" >>= strAdd "33"
let y = strToInt "1" >>= strAdd "n00" >>= strAdd "33"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment