Skip to content

Instantly share code, notes, and snippets.

@gubser
Last active November 6, 2018 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gubser/a0132509206063bc0560f18f6513e751 to your computer and use it in GitHub Desktop.
Save gubser/a0132509206063bc0560f18f6513e751 to your computer and use it in GitHub Desktop.
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