Say i want to return an Option
while in an async
workflow:
let run =
async {
let! x = doAsyncThing
let! y = doNextAsyncThing x
match y with
| None -> return None
| Some z -> return Some <| f z
}
Ideally I would use the maybe computation expression from FSharpx at the same time as async to avoid doing the match
. I could make a custom builder, but is there a way to generically combine two computation expressions? It might look something like this:
let run =
async {
let! x = doAsyncThing
let! y = doNextAsyncThing x
return! f y
}
A simple way to do so is to use Option module:
I suppose you don't have to deal with
option
in context ofasync
so often. FSharpx also provides many more high-order functions foroption
type. Most of the cases, I think using them is enough.To get the feeling of using these functions, please take a look at this nice article.
Typically in F# instead of using generic workflows you define the workflow by hand, or use one that is ready available as in your case
async
andmaybe
but if you want to use them combined you will need to code a specific workflow combination by hand.Alternatively you can use F#+ which is a project that provides generic workflows for monads, in that case it will be automatically derived for you, here's a working example, using your workflow and then using
OptionT
which is a monad transformer:The first function has to be 'lifted' into the other monad, because it only deals with
Async
(not withOption
), the second function deals with both so it only needs to be 'packed' into ourOptionT
DU.As you can see both workflows are derived automatically, the one you had (the async workflow) and the one you want.
For more information about this approach read about Monad Transformers.
just use f# Computation expressions inside.