Skip to content

How to Replace a Foreach Loop with LanguageExt in Async Code Involving Database Operations? #1404

Answered by louthy
padnom asked this question in Q&A
Discussion options

You must be logged in to vote

First off, make sure you have the latest beta, it needed some changes to support this for IO-based operations.

No, don't use Map, use Traverse:

// Two failing Eff effects, note how the Task is lifted into the Eff.  
var eff1 = liftEff(e => Task.FromException<int>(Error.New("failed 1")));
var eff2 = liftEff(e => Task.FromException<int>(Error.New("failed 2")));

// Put the two effects into a Seq and Traverse
var eff3 = Seq(eff1, eff2).Traverse(x => x);

// Run the effect to get the result
var res  = await eff3.RunAsync();

Console.WriteLine(res);   // Fail([failed 1, failed 2])

I note you've changed the requirements to "retrieve both errors and successes."; the above will only return either…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@louthy
Comment options

Answer selected by padnom
@padnom
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants