Skip to content

Instantly share code, notes, and snippets.

@andybrackley
Created February 23, 2014 19:54
Show Gist options
  • Save andybrackley/9176410 to your computer and use it in GitHub Desktop.
Save andybrackley/9176410 to your computer and use it in GitHub Desktop.
(* Maybe Monad *)
(*
Taken from FSharpX:
https://github.com/fsprojects/fsharpx/blob/master/src/FSharpx.Core/ComputationExpressions/Monad.fs
*)
type MaybeBuilder() =
member this.Return(x) = Some x
member this.ReturnFrom(m: 'T option) = m
member this.Zero() = None
member this.Bind(m, f) = Option.bind f m
let maybe = MaybeBuilder()
let res =
maybe {
let! x1 = Some(10) // maybe.Bind(Some(10), fun a ->
let! x2 = None // maybe.Bind(None, fun b ->
return! Some(x1 + x2) // a + b Using Option
}
printf "%A" res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment