Skip to content

Instantly share code, notes, and snippets.

@vasily-kirichenko
Created November 13, 2012 16:49
Show Gist options
  • Save vasily-kirichenko/4066913 to your computer and use it in GitHub Desktop.
Save vasily-kirichenko/4066913 to your computer and use it in GitHub Desktop.
F# maybe monad
type Maybe() =
member __.Bind(value, rest) =
if value = null then null else rest value
member __.Return(value) =
value
let maybe = new Maybe()
let substring (str: string) (from: int) =
maybe {
let! x = str
let! substr = x.Substring from
return "[" + substr + "]"
}
// test it with not null string
> substring "1234" 2;;
> val it : string = "[34]"
// and now with null string
> substring null 2;;
> val it : string = null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment