Missing some functions in OptionAsync #1274
Unanswered
JosVroegindeweij
asked this question in
Q&A
Replies: 1 comment
-
You could try this approach instead: using public EitherAsync<Error, Unit> MainFunction(Command cmd) =>
// Early out is good
from data1 in GetData1(cmd)
from data2 in GetData2(cmd)
from data3 in GetData3(cmd)
from data4 in GetData4(cmd)
// No early out anymore
let process1 = ProcessData1(data1)
let process2 = ProcessData2(data2)
let process3 = ProcessData3(data3)
let process4 = ProcessData4(data4)
// Collect the lefts from the processes. If there aren't any
// then return Right, else collect the Errors together and return
// Left
from result in Seq(process1, process2, process3, process4)
.Lefts()
.Map(errors => errors.IsEmpty
? Right<Error, Unit>(unit)
: Left<Error, Unit>(Error.Many(errors)))
.ToAsync()
select result; NOTE: This is using the Really you're trying to do the work of a validation monad rather than an either here. You might want to consider developing a more bespoke monad for your use-case, or use language-ext's |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello there,
Thanks for making this library, it made me get back into the functional programming world again, but university is so long ago :)
I'm trying to come up with an elegant solution for the following problem:
EitherAsync<Error, byte[]> GetDataX(Command cmd)
).OptionAsync<Error> ProcessDataX(byte[])
)When errors are encountered in step 1, we should early exit. When errors are encountered in step 2, we should not early exit. In the end, I want to end up with an
OptionAsync<Error>
, where in the error all errors are accumulated (either a single error if it got returned fromGetDataX
or a accumulated error if it happened in one or more ofProcessDataX
)I have managed to make the following work (as a simplified example), but I hope this is not the only way
A few things I dislike about this:
MainFunction
and theProcessDataX
functions return anEitherAsync<Error, Unit>
instead of anOptionAsync<Error>
. I've tried getting the same results with anOptionAsync<Error>
but there is no functionOptionAsync<A>.BiBind()
in the library and trying to write it myself outside of the library went horribly wrong :)Any help, considerations or pointers would be greatly appreciated!
Edit: I just noticed the title is not very descriptive of the rest of the story I just wrote. The title is about the lack of the
BiBind
andBiBindAsync
methods onOptionAsync
.Also, I just read the proposal for making the
OptionAsync
andEitherAsync
obsolete. How could I use theAff
as proposed there in this use case?Beta Was this translation helpful? Give feedback.
All reactions