Created
January 27, 2016 22:16
-
-
Save johnazariah/438078ff6d3d9f1ba452 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[<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