Last active
November 6, 2018 21:19
-
-
Save gubser/a0132509206063bc0560f18f6513e751 to your computer and use it in GitHub Desktop.
Helper function to chain TaskResult producing functions
This file contains 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
type TaskResult<'a,'b> = Task<Result<'a,'b>> | |
module TaskResult = | |
let bind (f: 'a -> TaskResult<'b,'e>) (x: TaskResult<'a,'e>): TaskResult<'b,'e> = task { | |
match! x with | |
| Ok success -> return! f success | |
| Error failure -> return Error failure | |
} | |
// should it be called bindResult or bindTask? | |
let bindResult (f: 'a -> Result<'b,'e>) (x: TaskResult<'a,'e>): TaskResult<'b,'e> = task { | |
let! y = x | |
return Result.bind f y | |
} | |
let mapError (f: 'e -> 'f) (x: TaskResult<'a,'e>): TaskResult<'a,'f> = task { | |
match! x with | |
| Ok success -> return Ok success | |
| Error failure -> return Error (f failure) | |
} | |
let add2 a: TaskResult<int, string> = task { | |
return Ok(a + 2) | |
} | |
let mul5 a: TaskResult<int, string> = task { | |
return Ok(a * 5) | |
} | |
let combo a: TaskResult<int, string> = | |
a | |
|> add2 | |
|> TaskResult.bind mul5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment