Skip to content

Instantly share code, notes, and snippets.

@gubser gubser/TaskResultBind.fs
Last active Nov 6, 2018

Embed
What would you like to do?
Helper function to chain TaskResult producing functions
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
You can’t perform that action at this time.