Skip to content

Instantly share code, notes, and snippets.

@mollyporph
Created October 5, 2015 14:12
Show Gist options
  • Save mollyporph/4510d4c7a5050a838286 to your computer and use it in GitHub Desktop.
Save mollyporph/4510d4c7a5050a838286 to your computer and use it in GitHub Desktop.
rwp bindings
type Res<'TSuccess,'TFailure> =
| Success of 'TSuccess
| Failure of 'TFailure
let succeed x =
Success x
let fail x =
Failure x
let either successFunc failureFunc x2Rop =
match x2Rop with
| Success s -> successFunc s
| Failure f -> failureFunc f
let bind f =
either f fail
let toSwitch f =
f >> succeed
let map f =
either (f >> succeed) fail
//d-e to 1w
let tee f x=
f x; x
let tryCatch f exnHandler x =
try
f x |> succeed
with
| ex -> exnHandler ex |> fail
let bimap successFunc failureFunc =
either (successFunc >> succeed) (failureFunc >> fail)
let combine biSuccess biFailure leftSwitch rightSwitch x =
match (leftSwitch x),(rightSwitch x) with
| Success s1,Success s2 -> Success (biSuccess s1 s2)
| Failure f1,Success _ -> Failure f1
| Success _ ,Failure f2 -> Failure f2
| Failure f1,Failure f2 -> Failure (biFailure f1 f2)
//string concat on failure-case,r1 r2 => r1 on Success
let (&&&) v1 v2 =
let addSuccess r1 r2 = r1
let addFailure s1 s2 = s1 + "; " + s2
combine addSuccess addFailure v1 v2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment