Skip to content

Instantly share code, notes, and snippets.

@pirrmann
Created October 14, 2016 12:30
Show Gist options
  • Save pirrmann/868797784136473a51163b21d61f6295 to your computer and use it in GitHub Desktop.
Save pirrmann/868797784136473a51163b21d61f6295 to your computer and use it in GitHub Desktop.
type Result<'T> =
| Success of 'T
| Failure of string
let bind f = function | Success a -> f a | Failure b -> Failure b
let map f = function | Success a -> Success (f a) | Failure b -> Failure b
let private combine2 fn2 fn1 arg =
match (fn1 arg, fn2 arg) with
| Success a, Success b -> Success (a, b)
| Failure a, Success _ -> Failure a
| Success _, Failure b -> Failure b
| Failure a, Failure b -> Failure <| a + " " + b
type Combiner = Combiner with
static member inline (?<-) (_, _, (fn1, fn2)) =
fn1 |> combine2 fn2
static member inline (?<-) (_, _, (fn1, fn2, fn3)) =
fn1 |> combine2 fn2 |> combine2 fn3
>> map (function | (a, b), c -> a, b, c)
static member inline (?<-) (_, _, (fn1, fn2, fn3, fn4)) =
fn1 |> combine2 fn2 |> combine2 fn3 |> combine2 fn4
>> map (function | ((a, b), c), d -> a, b, c, d)
static member inline (?<-) (_, _, (fn1, fn2, fn3, fn4, fn5)) =
fn1 |> combine2 fn2 |> combine2 fn3 |> combine2 fn4 |> combine2 fn5
>> map (function | (((a, b), c), d), e -> a, b, c, d, e)
static member inline (?<-) (_, _, (fn1, fn2, fn3, fn4, fn5, fn6)) =
fn1 |> combine2 fn2 |> combine2 fn3 |> combine2 fn4 |> combine2 fn5 |> combine2 fn6
>> map (function | ((((a, b), c), d), e), f -> a, b, c, d, e, f)
static member inline (?<-) (_, _, (fn1, fn2, fn3, fn4, fn5, fn6, fn7)) =
fn1 |> combine2 fn2 |> combine2 fn3 |> combine2 fn4 |> combine2 fn5 |> combine2 fn6 |> combine2 fn7
>> map (function | (((((a, b), c), d), e), f), g -> a, b, c, d, e, f, g)
let inline bindMany funs = (?<-) Combiner () funs
let succeed x = Success x
let fail i x = sprintf "failed%d" i |> Result<int>.Failure
let test = 42 |> bindMany (fail 1, fail 2, fail 3, fail 4, fail 5, fail 6, fail 7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment