Skip to content

Instantly share code, notes, and snippets.

@johnazariah
Created January 27, 2016 22:16
Show Gist options
  • Save johnazariah/438078ff6d3d9f1ba452 to your computer and use it in GitHub Desktop.
Save johnazariah/438078ff6d3d9f1ba452 to your computer and use it in GitHub Desktop.
[<AutoOpen>]
module SuccessOrFailure =
type SuccessOrFailure<'a> =
| Success of 'a
| Failure of exn
let point v = Success v
let (>>=) m f =
match m with
| Success x ->
try
f x
with ex -> Failure ex
| Failure x -> Failure x
let (<!>) m f =
m >>= (fun x -> f x |> Success)
let extract = function
| Success x -> x
| Failure x -> raise x
type SuccessOrFailureBuilder () =
member this.Bind (m, f) = m >>= f
member this.Return v = point v
member this.Yield v = point v
member this.ReturnFrom m = m
member this.YieldFrom m = m
member this.Fail v = Failure v
member this.Zero() =
Success
let succeed = new SuccessOrFailureBuilder ()
let foldrM f z = List.fold (fun m a -> m >>= f a) (point z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment